feat: add postgres repository for training
This commit is contained in:
parent
8c277ef692
commit
ff7e320481
19 changed files with 1277 additions and 414 deletions
67
cmd/seed/main.go
Normal file
67
cmd/seed/main.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package main
|
||||
|
||||
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/bootstrap"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/faker"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||
"go.uber.org/zap"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
shutdownCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
logger := bootstrap.Logger()
|
||||
|
||||
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(shutdownCtx, 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)
|
||||
|
||||
f := faker.NewFaker(trainingRepo, trainingDateRepo)
|
||||
if err := f.GenerateFakeData(); err != nil {
|
||||
logger.Fatal("Error generating fake data", zap.Error(err))
|
||||
}
|
||||
|
||||
logger.Info("Fake data generated successfully!")
|
||||
}
|
||||
|
|
@ -2,36 +2,18 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/internal/bootstrap"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=../../oapi-codegen.yaml ../../api/v1/openapi.yaml
|
||||
|
||||
var port = 3000
|
||||
|
||||
func main() {
|
||||
shutdownCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
logger := zap.Must(zap.NewDevelopment())
|
||||
defer logger.Sync()
|
||||
|
||||
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)
|
||||
logger := bootstrap.Logger()
|
||||
srv := bootstrap.Bootstrap(logger)
|
||||
srv.Run(shutdownCtx)
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue