package main import ( "context" "time" "go.uber.org/fx" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/config" fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/controller/http/fiber" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/service" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/service/authentication" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/store/postgres" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/valkey" ) type dependencyReadiness struct { database fiberctrl.Readiness cache fiberctrl.Readiness objects fiberctrl.Readiness } // Ping checks persistence and all limiter shards under a shared deadline. func (d *dependencyReadiness) Ping(ctx context.Context) error { if err := d.database.Ping(ctx); err != nil { return err } if err := d.cache.Ping(ctx); err != nil { return err } if d.objects != nil { return d.objects.Ping(ctx) } return nil } // newDatabase creates the pool and registers its migration/start and shutdown hooks. func newDatabase(lifecycle fx.Lifecycle, cfg *config.Persistence) (*postgres.Store, error) { database, err := postgres.New(postgres.Options{ URL: cfg.DatabaseURL, MaxConnections: int32(cfg.PoolSize), Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond, }) if err != nil { return nil, err } lifecycle.Append(fx.Hook{ OnStart: database.CheckSchema, OnStop: func(context.Context) error { database.Close() return nil }, }) return database, nil } // newValkey creates bounded clients and registers connectivity and cleanup hooks. func newValkey(lifecycle fx.Lifecycle, cfg *config.Persistence) (*valkey.Client, error) { client, err := valkey.New(valkey.Options{ Nodes: cfg.Shards, Coordinator: cfg.Coordinator, PoolSize: cfg.PoolSize, Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond, }) if err != nil { return nil, err } lifecycle.Append(fx.Hook{ OnStart: client.Ping, OnStop: func(context.Context) error { return client.Close() }, }) return client, nil } // newAuthentication binds credentials and KDF implementations to authentication policy. func newAuthentication(accounts service.Accounts, passwords service.Passwords, cfg *config.Persistence) (service.Authentication, error) { return authentication.New(accounts, passwords, time.Duration(cfg.SessionTTLSeconds)*time.Second) } // newReadiness assembles infrastructure checks at the composition boundary. func newReadiness(database *postgres.Store, cache *valkey.Client) *dependencyReadiness { return &dependencyReadiness{database: database, cache: cache} }