78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service/authentication"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store/postgres"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/valkey"
|
|
)
|
|
|
|
type dependencyReadiness struct {
|
|
database *postgres.Store
|
|
cache *valkey.Client
|
|
}
|
|
|
|
// 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
|
|
}
|
|
return d.cache.Ping(ctx)
|
|
}
|
|
|
|
// 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}
|
|
}
|