forked from mtusi2026/mtusi2026lab3
Реализовать lab3 в общем Helm-релизе с Traefik, TLS и HPA
This commit is contained in:
+6
-1
@@ -43,7 +43,7 @@ func CreateApp() fx.Option {
|
||||
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(cursor.New, fx.As(new(service.CursorCodec))),
|
||||
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))),
|
||||
),
|
||||
@@ -95,3 +95,8 @@ func databaseAccounts(database *postgres.Store) service.Accounts {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -101,4 +101,8 @@ func requirePersistence(t *testing.T) {
|
||||
if os.Getenv("DATABASE_URL") == "" || os.Getenv("VALKEY_SHARDS") == "" {
|
||||
t.Skip("DATABASE_URL and VALKEY_SHARDS are required for real application lifecycle tests")
|
||||
}
|
||||
t.Setenv("CURSOR_SIGNING_KEY", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
if err := migrate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
+23
-1
@@ -1,7 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store/postgres"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
@@ -12,8 +17,25 @@ func run(args []string) error {
|
||||
fx.New(CreateApp()).Run()
|
||||
return nil
|
||||
}
|
||||
if len(args) == 1 && args[0] == "migrate" {
|
||||
return migrate()
|
||||
}
|
||||
if len(args) == 1 && args[0] == "healthcheck" {
|
||||
return healthcheck()
|
||||
}
|
||||
return fmt.Errorf("usage: server [healthcheck]")
|
||||
return fmt.Errorf("usage: server [healthcheck|migrate]")
|
||||
}
|
||||
|
||||
// migrate applies versioned DDL once from an operator job, independently of the API.
|
||||
func migrate() error {
|
||||
cfg, err := config.NewPersistence()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 30 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer database.Close()
|
||||
return database.Start(context.Background())
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func newDatabase(lifecycle fx.Lifecycle, cfg *config.Persistence) (*postgres.Sto
|
||||
return nil, err
|
||||
}
|
||||
lifecycle.Append(fx.Hook{
|
||||
OnStart: database.Start,
|
||||
OnStart: database.CheckSchema,
|
||||
OnStop: func(context.Context) error {
|
||||
database.Close()
|
||||
return nil
|
||||
|
||||
@@ -46,3 +46,21 @@ func TestReadinessStopsAtUnavailableDatabase(t *testing.T) {
|
||||
t.Fatal("closed database reported ready")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationCommandRejectsMissingConfiguration fails before opening a database.
|
||||
func TestMigrationCommandRejectsMissingConfiguration(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "")
|
||||
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
|
||||
if err := run([]string{"migrate"}); err == nil {
|
||||
t.Fatal("missing database configuration accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationCommandRejectsMalformedDatabaseURL fails without logging credentials.
|
||||
func TestMigrationCommandRejectsMalformedDatabaseURL(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "://invalid")
|
||||
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
|
||||
if err := run([]string{"migrate"}); err == nil {
|
||||
t.Fatal("malformed database configuration accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user