forked from mtusi2026/mtusi2026lab3
103 lines
3.8 KiB
Go
103 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/admission"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/controller"
|
|
fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/controller/http/fiber"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/password"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service/production"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store/postgres"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/valkey"
|
|
)
|
|
|
|
const applicationStopTimeout = 9 * time.Second
|
|
|
|
// CreateApp defines the dependency graph and Fx lifecycle for the lab2 server.
|
|
// Constructors do not open sockets; controller.RunControllerFx registers startup
|
|
// and shutdown hooks. The outer stop deadline bounds all component cleanup.
|
|
func CreateApp() fx.Option {
|
|
return fx.Options(
|
|
fx.StopTimeout(applicationStopTimeout),
|
|
fx.Supply(fx.Annotate(password.Hasher{}, fx.As(new(service.Passwords)))),
|
|
fx.Provide(
|
|
config.New,
|
|
config.NewPersistence,
|
|
newDatabase,
|
|
newValkey,
|
|
newAuthentication,
|
|
fx.Annotate(newReadiness, fx.As(new(fiberctrl.Readiness))),
|
|
httpConfig,
|
|
|
|
loggerConfig,
|
|
newLogger,
|
|
fx.Annotate(newAdmissionLimiter, fx.As(new(admission.Distributed))),
|
|
fiberctrl.NewErrorRegistry,
|
|
fx.Annotate(databaseStore, fx.As(new(store.Interface))),
|
|
fx.Annotate(databaseAccounts, fx.As(new(service.Accounts))),
|
|
fx.Annotate(profileCache, fx.As(new(service.ProfileCache))),
|
|
fx.Annotate(newCursor, fx.As(new(service.CursorCodec))),
|
|
fx.Annotate(production.NewCached, fx.As(new(service.Service))),
|
|
fx.Annotate(fiberctrl.New, fx.As(new(controller.Controller))),
|
|
),
|
|
fx.WithLogger(newFxLogger),
|
|
fx.Invoke(controller.RunControllerFx),
|
|
)
|
|
}
|
|
|
|
// httpConfig provides the HTTP configuration section to Fx.
|
|
func httpConfig(cfg *config.Config) *config.Controller {
|
|
return cfg.HTTP
|
|
}
|
|
|
|
// loggerConfig provides the logging configuration section to Fx.
|
|
func loggerConfig(cfg *config.Config) *config.Logger {
|
|
return cfg.Logger
|
|
}
|
|
|
|
// newAdmissionLimiter translates configured bounds into separate shared policy budgets.
|
|
func newAdmissionLimiter(cfg *config.Controller, client *valkey.Client) (*valkey.Limiter, error) {
|
|
global := valkey.Budget{Rate: cfg.Admission.RequestsPerSecond, Slots: cfg.Admission.MaxInFlight}
|
|
return valkey.NewLimiter(client, valkey.Limits{
|
|
Global: global,
|
|
Groups: map[string]valkey.Budget{
|
|
"auth": {Rate: cfg.Admission.AuthRate, Slots: cfg.Admission.AuthSlots},
|
|
"read": global,
|
|
"write": global,
|
|
},
|
|
User: valkey.Budget{Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots},
|
|
UserGroups: map[string]valkey.Budget{
|
|
"auth": {Rate: cfg.Admission.UserAuthRate, Slots: cfg.Admission.UserAuthSlots},
|
|
"read": {Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots},
|
|
"write": {Rate: cfg.Admission.UserWriteRate, Slots: cfg.Admission.UserWriteSlots},
|
|
},
|
|
})
|
|
}
|
|
|
|
// databaseStore exposes business persistence through its consumer interface.
|
|
func databaseStore(database *postgres.Store) store.Interface {
|
|
return database
|
|
}
|
|
|
|
// databaseAccounts exposes account and session persistence through its consumer interface.
|
|
func databaseAccounts(database *postgres.Store) service.Accounts {
|
|
return database
|
|
}
|
|
|
|
// profileCache exposes immutable profiles without leaking the Valkey client.
|
|
func profileCache(client *valkey.Client) service.ProfileCache {
|
|
return client
|
|
}
|
|
|
|
// newCursor binds the shared signing secret at the composition root.
|
|
func newCursor(cfg *config.Persistence) (*cursor.Codec, error) {
|
|
return cursor.NewShared(cfg.CursorKey)
|
|
}
|