package config import ( "fmt" "os" "strings" ) // Persistence contains database, cache and session settings. type Persistence struct { // DatabaseURL identifies the PostgreSQL database. DatabaseURL string // Shards maps stable identities to Valkey addresses. Shards map[string]string // Coordinator names the stable global admission shard. Coordinator string // PoolSize bounds connections per dependency. PoolSize int // TimeoutMilliseconds bounds each dependency operation. TimeoutMilliseconds int // SessionTTLSeconds sets the absolute session lifetime. SessionTTLSeconds int } // NewPersistence loads dependency settings separately from HTTP configuration. func NewPersistence() (*Persistence, error) { cfg := &Persistence{ DatabaseURL: os.Getenv("DATABASE_URL"), Coordinator: os.Getenv("VALKEY_COORDINATOR"), Shards: make(map[string]string), PoolSize: 8, TimeoutMilliseconds: 1000, SessionTTLSeconds: 3600, } if cfg.Coordinator == "" { cfg.Coordinator = "shard0" } for _, entry := range strings.Split(os.Getenv("VALKEY_SHARDS"), ",") { id, address, ok := strings.Cut(entry, "=") if !ok || id == "" || address == "" || cfg.Shards[id] != "" { return nil, fmt.Errorf("VALKEY_SHARDS requires unique id=address entries") } cfg.Shards[id] = address } if cfg.DatabaseURL == "" || cfg.Shards[cfg.Coordinator] == "" { return nil, fmt.Errorf("DATABASE_URL and a configured VALKEY_COORDINATOR are required") } if err := loadIntegers(integerSetting{"DEPENDENCY_POOL_SIZE", &cfg.PoolSize}, integerSetting{"DEPENDENCY_TIMEOUT_MS", &cfg.TimeoutMilliseconds}, integerSetting{"SESSION_TTL_SECONDS", &cfg.SessionTTLSeconds}); err != nil { return nil, err } return cfg, nil }