package config import ( "log" "log/slog" "os" "strconv" ) type Config struct { Server ServerConfig Logging LoggingConfig } type ServerConfig struct { Host string Port string MaxInflightRequests int } type LoggingConfig struct { Level slog.Level } func Load() Config { return Config{ Server: ServerConfig{ Host: getEnv("APP_HOST", "0.0.0.0"), Port: getEnv("APP_PORT", "8080"), MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0), }, Logging: LoggingConfig{ Level: getEnvLogLevel("LOG_LEVEL", slog.LevelInfo), }, } } func getEnv(key, def string) string { value := os.Getenv(key) if value == "" { return def } return value } func getEnvNonNegativeInt(key string, def int) int { v := os.Getenv(key) if v == "" { return def } value, err := strconv.Atoi(v) if err != nil || value < 0 { log.Printf("invalid non-negative int for %s=%q, using default %d", key, v, def) return def } return value } func getEnvLogLevel(key string, def slog.Level) slog.Level { v := os.Getenv(key) switch v { case "DEBUG": return slog.LevelDebug case "INFO": return slog.LevelInfo case "WARN": return slog.LevelWarn case "ERROR": return slog.LevelError case "": return def default: log.Printf("invalid log level for %s=%q, using default %s", key, v, def) return def } }