package config import ( "log/slog" "os" "strconv" "strings" "time" ) type Config struct { Host string Port string Env string SessionTTLSeconds int DatabaseURL string CacheURL string SessionStore string LogLevel slog.Level ShutdownTimeout time.Duration CacheEnabled bool } func Load() Config { return Config{ Host: getEnv("APP_HOST", "0.0.0.0"), Port: getEnv("APP_PORT", "8080"), Env: getEnv("APP_ENV", "lab"), SessionTTLSeconds: getInt("SESSION_TTL_SECONDS", 3600), DatabaseURL: readSecretFile("DATABASE_URL_FILE"), CacheURL: readSecretFile("CACHE_URL_FILE"), SessionStore: getEnv("SESSION_STORE", "postgres"), LogLevel: parseLevel(getEnv("LOG_LEVEL", "info")), ShutdownTimeout: parseDuration(getEnv("SHUTDOWN_TIMEOUT", "8s")), CacheEnabled: getEnv("CACHE_ENABLED", "true") == "true", } } func (c Config) SessionTTL() time.Duration { return time.Duration(c.SessionTTLSeconds) * time.Second } func (c Config) Addr() string { return c.Host + ":" + c.Port } func getEnv(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def } func getInt(key string, def int) int { if v := os.Getenv(key); v != "" { if n, err := strconv.Atoi(v); err == nil { return n } } return def } func readSecretFile(envKey string) string { path := os.Getenv(envKey) if path == "" { return "" } b, err := os.ReadFile(path) if err != nil { slog.Warn("secret file not readable", "path", path, "err", err) return "" } return strings.TrimSpace(string(b)) } func parseLevel(s string) slog.Level { var l slog.Level if err := l.UnmarshalText([]byte(s)); err != nil { return slog.LevelInfo } return l } func parseDuration(s string) time.Duration { d, err := time.ParseDuration(s) if err != nil { return 8 * time.Second } return d }