// Package config reads the runtime configuration shared by every API replica. package config import ( "log" "os" "strconv" "time" ) const ( defaultPort = "8080" defaultSessionTTL = time.Hour defaultIdempotencyTTL = 24 * time.Hour defaultDrainDelay = 3 * time.Second ) // Config is the complete runtime configuration of one API process. type Config struct { InstanceID string Port string DatabaseURL string SessionTTL time.Duration IdempotencyTTL time.Duration DrainDelay time.Duration } // Load reads the environment; DATABASE_URL is the only mandatory value. func Load() *Config { cfg := &Config{ InstanceID: valueOr(os.Getenv("APP_INSTANCE_ID"), "api"), Port: valueOr(os.Getenv("APP_PORT"), defaultPort), DatabaseURL: os.Getenv("DATABASE_URL"), SessionTTL: durationOr("SESSION_TTL_SECONDS", defaultSessionTTL), IdempotencyTTL: durationOr("IDEMPOTENCY_TTL_SECONDS", defaultIdempotencyTTL), DrainDelay: durationOr("DRAIN_DELAY_SECONDS", defaultDrainDelay), } if cfg.DatabaseURL == "" { log.Fatal("config: DATABASE_URL is required") } return cfg } func valueOr(raw, fallback string) string { if raw == "" { return fallback } return raw } func durationOr(env string, fallback time.Duration) time.Duration { raw := os.Getenv(env) if raw == "" { return fallback } seconds, err := strconv.Atoi(raw) if err != nil || seconds <= 0 { log.Printf("config: ignoring invalid %s=%q, using %s", env, raw, fallback) return fallback } return time.Duration(seconds) * time.Second }