feat: add postgres repository for training
This commit is contained in:
		
							parent
							
								
									8c277ef692
								
							
						
					
					
						commit
						ff7e320481
					
				
					 19 changed files with 1277 additions and 414 deletions
				
			
		
							
								
								
									
										95
									
								
								internal/bootstrap/bootstrap.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								internal/bootstrap/bootstrap.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,95 @@ | |||
| package bootstrap | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	pgxDeciaml "github.com/jackc/pgx-shopspring-decimal" | ||||
| 	"github.com/jackc/pgx/v5" | ||||
| 	"github.com/jackc/pgx/v5/pgxpool" | ||||
| 	pgxUUID "github.com/vgarvardt/pgx-google-uuid/v5" | ||||
| 	"gitlab.mareshq.com/hq/yggdrasil/internal/faker" | ||||
| 	"gitlab.mareshq.com/hq/yggdrasil/internal/server" | ||||
| 	"gitlab.mareshq.com/hq/yggdrasil/pkg/training" | ||||
| 	"go.uber.org/zap" | ||||
| 	"os" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| func Bootstrap(logger *zap.Logger) *server.Server { | ||||
| 	portStr := os.Getenv("APP_PORT") | ||||
| 	if portStr == "" { | ||||
| 		logger.Fatal("APP_PORT is not set") | ||||
| 	} | ||||
| 
 | ||||
| 	port, err := strconv.Atoi(portStr) | ||||
| 	if err != nil { | ||||
| 		logger.Fatal("Error parsing APP_PORT", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	databaseURLFile := os.Getenv("DATABASE_URL_FILE") | ||||
| 	if databaseURLFile == "" { | ||||
| 		logger.Fatal("DATABASE_URL_FILE is not set") | ||||
| 	} | ||||
| 
 | ||||
| 	dat, err := os.ReadFile(databaseURLFile) | ||||
| 	if err != nil { | ||||
| 		logger.Fatal("Error reading DATABASE_URL_FILE", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	databaseURL := string(dat) | ||||
| 
 | ||||
| 	// clean up url from invalid characters | ||||
| 	databaseURL = strings.ReplaceAll(databaseURL, "\n", "") | ||||
| 	databaseURL = strings.ReplaceAll(databaseURL, "\t", "") | ||||
| 
 | ||||
| 	pgxConfig, err := pgxpool.ParseConfig(databaseURL) | ||||
| 	if err != nil { | ||||
| 		logger.Fatal("Error parsing database url with pgx (database driver)", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	pgxConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { | ||||
| 		pgxUUID.Register(conn.TypeMap()) | ||||
| 		pgxDeciaml.Register(conn.TypeMap()) | ||||
| 		return nil | ||||
| 	} | ||||
| 
 | ||||
| 	pool, err := pgxpool.NewWithConfig(context.TODO(), pgxConfig) | ||||
| 	if err != nil { | ||||
| 		logger.Fatal("Error creating pgx pool", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	trainingRepo := training.NewPostgresTrainingRepository(pool) | ||||
| 	trainingDateRepo := training.NewPostgresTrainingDateRepository(pool) | ||||
| 	trainingDateAttendeeRepo := training.NewPostgresTrainingDateAttendeeRepository(pool) | ||||
| 
 | ||||
| 	apiHandlers := server.NewAPIHandlers(trainingRepo, trainingDateRepo, trainingDateAttendeeRepo) | ||||
| 
 | ||||
| 	srv := server.NewServer(apiHandlers, port, logger, pool) | ||||
| 
 | ||||
| 	return srv | ||||
| } | ||||
| 
 | ||||
| func BootstrapInMemory(logger *zap.Logger) *server.Server { | ||||
| 	portStr := os.Getenv("APP_PORT") | ||||
| 	if portStr == "" { | ||||
| 		logger.Fatal("APP_PORT is not set") | ||||
| 	} | ||||
| 	port, err := strconv.Atoi(portStr) | ||||
| 	if err != nil { | ||||
| 		logger.Fatal("Error parsing APP_PORT", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	trainingRepository := training.NewInMemoryTrainingRepository() | ||||
| 	trainingDateRepository := training.NewInMemoryTrainingDateRepository() | ||||
| 	trainingDateAttendeeRepository := training.NewInMemoryTrainingDateAttendeeRepository() | ||||
| 
 | ||||
| 	f := faker.NewFaker(trainingRepository, trainingDateRepository) | ||||
| 	if err := f.GenerateFakeData(); err != nil { | ||||
| 		logger.Fatal("Error generating fake data", zap.Error(err)) | ||||
| 	} | ||||
| 
 | ||||
| 	apiHandlers := server.NewAPIHandlers(trainingRepository, trainingDateRepository, trainingDateAttendeeRepository) | ||||
| 	srv := server.NewServer(apiHandlers, port, logger, nil) | ||||
| 
 | ||||
| 	return srv | ||||
| } | ||||
		Reference in a new issue