2025-01-10 11:12:03 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
"queryorchestration/internal/database"
|
2025-03-11 18:15:49 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/build"
|
2025-03-06 22:33:39 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-10 11:12:03 +00:00
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-05 20:03:36 +00:00
|
|
|
const DEFAULT_SECRET_PREFIX = "secret"
|
|
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
type Config interface {
|
|
|
|
|
serviceconfig.ConfigProvider
|
|
|
|
|
SetValidator()
|
|
|
|
|
GetValidator() *validator.Validate
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
type BaseConfig struct {
|
|
|
|
|
serviceconfig.BaseConfig
|
|
|
|
|
Validator *validator.Validate
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
func (c *BaseConfig) SetValidator() {
|
|
|
|
|
valid := validator.New()
|
2025-01-10 11:12:03 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
c.Validator = valid
|
|
|
|
|
}
|
|
|
|
|
func (c *BaseConfig) GetValidator() *validator.Validate {
|
|
|
|
|
return c.Validator
|
|
|
|
|
}
|
2025-01-10 11:12:03 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
func New(ctx context.Context, cfg Config) (func() error, error) {
|
2025-02-05 20:03:36 +00:00
|
|
|
// init the config in case it has not already been called.
|
|
|
|
|
errInitializingConfig := serviceconfig.InitializeConfig(cfg)
|
|
|
|
|
if errInitializingConfig != nil {
|
|
|
|
|
return func() error { return nil }, errInitializingConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg.PrintConfig(DEFAULT_SECRET_PREFIX)
|
2025-02-03 19:01:37 +00:00
|
|
|
closeTracer := cfg.SetOtel(ctx)
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-03-11 18:15:49 +00:00
|
|
|
version := build.GetVersion()
|
2025-03-06 22:33:39 +00:00
|
|
|
cfg.GetLogger().Info("Starting", "version", version)
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
err := database.RunMigrations(ctx, cfg)
|
2025-01-31 13:43:55 +00:00
|
|
|
if err != nil {
|
2025-02-03 17:30:50 +00:00
|
|
|
return func() error { return nil }, err
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg.SetValidator()
|
|
|
|
|
|
|
|
|
|
err = cfg.SetDBPool(ctx)
|
2025-01-10 19:17:20 +00:00
|
|
|
if err != nil {
|
2025-02-03 17:30:50 +00:00
|
|
|
return func() error { return nil }, err
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
2025-01-10 11:12:03 +00:00
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
err = cfg.SetQueueClient(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
return closeTracer, nil
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|