package config import ( "fmt" "log" "log/slog" "os" "strconv" "strings" "time" ) const ( SessionStorePostgres = "postgres" SessionStoreRedis = "redis" ) type Config struct { Server ServerConfig Logging LoggingConfig Database DatabaseConfig Redis RedisConfig S3 S3Config SessionStore string } func (c Config) UseCache() bool { return c.SessionStore == SessionStoreRedis } type ServerConfig struct { Host string Port string MaxInflightRequests int SessionTTL time.Duration IdempotencyTTL time.Duration InstanceID string } type LoggingConfig struct { Level slog.Level } type DatabaseConfig struct { URLFile string } func (c DatabaseConfig) URL() (string, error) { return readSecretFile(c.URLFile) } type RedisConfig struct { URLFile string } func (c RedisConfig) URL() (string, error) { return readSecretFile(c.URLFile) } type S3Config struct { Endpoint string Region string Bucket string APIAccessKeyFile string APISecretKeyFile string BootstrapAccessKeyFile string BootstrapSecretKeyFile string MaxUploadBytes int MaxImagePixels int } func (c S3Config) APIAccessKey() (string, error) { return readSecretFile(c.APIAccessKeyFile) } func (c S3Config) APISecretKey() (string, error) { return readSecretFile(c.APISecretKeyFile) } func (c S3Config) BootstrapAccessKey() (string, error) { return readSecretFile(c.BootstrapAccessKeyFile) } func (c S3Config) BootstrapSecretKey() (string, error) { return readSecretFile(c.BootstrapSecretKeyFile) } 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), SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second, IdempotencyTTL: time.Duration(getEnvPositiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second, InstanceID: getInstanceID("INSTANCE_ID"), }, Logging: LoggingConfig{ Level: getEnvLogLevel("LOG_LEVEL", slog.LevelInfo), }, Database: DatabaseConfig{ URLFile: getEnv("DATABASE_URL_FILE", "/run/secrets/database_url"), }, Redis: RedisConfig{ URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"), }, S3: LoadS3(), SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres), } } func LoadS3() S3Config { return S3Config{ Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"), Region: getEnv("S3_REGION", "us-east-1"), Bucket: getEnv("S3_BUCKET", "messenger-attachments"), APIAccessKeyFile: getEnv("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"), APISecretKeyFile: getEnv("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"), BootstrapAccessKeyFile: getEnv("S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", "/run/secrets/s3_bootstrap_access_key_id"), BootstrapSecretKeyFile: getEnv("S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_bootstrap_secret_access_key"), MaxUploadBytes: getEnvPositiveInt("MAX_UPLOAD_BYTES", 10485760), MaxImagePixels: getEnvPositiveInt("MAX_IMAGE_PIXELS", 40000000), } } 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 getEnvPositiveInt(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 positive 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 } } func readSecretFile(path string) (string, error) { b, err := os.ReadFile(path) if err != nil { return "", fmt.Errorf("read secret file %s: %w", path, err) } value := strings.TrimSpace(string(b)) if value == "" { return "", fmt.Errorf("secret file %s is empty", path) } return value, nil } func getEnvSessionStore(key, def string) string { v := strings.ToLower(os.Getenv(key)) switch v { case "": return def case SessionStorePostgres, SessionStoreRedis: return v default: log.Printf("invalid session store for %s=%q, using default %s", key, v, def) return def } } func getInstanceID(key string) string { if v := os.Getenv(key); v != "" { return v } host, err := os.Hostname() if err != nil || host == "" { log.Printf("cannot determine hostname, using \"unknown\" for %s: %v", key, err) return "unknown" } return host }