Merged in feature/serviceconfig-integration (pull request #38)
DRAFT PR : WIP working through ideas for integration * movearound * attempttwo * openapi * further sanding * fix * start on tests * runthroughsingleconfig * somechanges * reflectissue * removeerrs * mostlyremovepanic * removeenv * noncfgtests * go * repo * fix service config test * add PWD to all * test fix * fix lint * todo for later * passingunittests * alltests * testlogger * testloggername * clean
This commit is contained in:
+33
-33
@@ -2,47 +2,47 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/database/migrations"
|
||||
"queryorchestration/internal/server/otel"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Database *database.Connection
|
||||
type Config interface {
|
||||
serviceconfig.ConfigProvider
|
||||
SetValidator()
|
||||
GetValidator() *validator.Validate
|
||||
}
|
||||
|
||||
type BaseConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
Validator *validator.Validate
|
||||
}
|
||||
|
||||
type NewConfig struct {
|
||||
BasePath string
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg *NewConfig) *Config {
|
||||
closeTracer := otel.New(ctx)
|
||||
defer closeTracer()
|
||||
|
||||
database.RunMigrations(ctx, &database.MigrationConfig{
|
||||
BasePath: cfg.BasePath,
|
||||
})
|
||||
|
||||
func (c *BaseConfig) SetValidator() {
|
||||
valid := validator.New()
|
||||
|
||||
dbPool := database.GetDBPool(ctx)
|
||||
dbQueries := repository.New(dbPool)
|
||||
db := &database.Connection{
|
||||
Pool: dbPool,
|
||||
Queries: dbQueries,
|
||||
}
|
||||
|
||||
err := dbPool.Ping(ctx)
|
||||
if err != nil {
|
||||
log.Panic("Unable to ping database")
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Database: db,
|
||||
Validator: valid,
|
||||
}
|
||||
c.Validator = valid
|
||||
}
|
||||
func (c *BaseConfig) GetValidator() *validator.Validate {
|
||||
return c.Validator
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg Config) (func() error, error) {
|
||||
closeTracer := otel.New(ctx, cfg)
|
||||
|
||||
err := migrations.Run(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg.SetValidator()
|
||||
|
||||
err = cfg.SetDBPool(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return closeTracer, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user