// Package config loads and validates the app's configuration from environment // variables. Everything the app needs to run lives here — no config files, // no flags, just env vars (Docker Compose friendly). package config import ( "fmt" "os" ) type Config struct { Port string // DBDriver is "postgres" (default, production) or "sqlite" (local // dev/testing without a real Postgres instance). When sqlite, // DatabaseURL is a filesystem path to the DB file instead of a DSN. DBDriver string DatabaseURL string OIDCIssuerURL string OIDCClientID string OIDCClientSecret string OIDCRedirectURL string // PublicBaseURL is this app's own externally-visible URL, e.g. // https://garage.example.com. Used to build the OIDC redirect URL and to // decide whether to mark cookies Secure. PublicBaseURL string JWTSigningSecret string // StorageDriver is "s3" (default, production) or "local" (writes // uploads to disk — for testing without an S3 bucket). When local, // LocalStoragePath is where files are written. StorageDriver string LocalStoragePath string AWSRegion string S3Bucket string // AWS credentials are picked up by the SDK's default credential chain // (env vars, instance role, etc.) — we don't read them ourselves. } func Load() (*Config, error) { cfg := &Config{ Port: getEnv("PORT", "8080"), DBDriver: getEnv("DB_DRIVER", "postgres"), DatabaseURL: os.Getenv("DATABASE_URL"), OIDCIssuerURL: os.Getenv("OIDC_ISSUER_URL"), OIDCClientID: os.Getenv("OIDC_CLIENT_ID"), OIDCClientSecret: os.Getenv("OIDC_CLIENT_SECRET"), OIDCRedirectURL: os.Getenv("OIDC_REDIRECT_URL"), PublicBaseURL: os.Getenv("PUBLIC_BASE_URL"), JWTSigningSecret: os.Getenv("JWT_SIGNING_SECRET"), StorageDriver: getEnv("STORAGE_DRIVER", "s3"), LocalStoragePath: getEnv("LOCAL_STORAGE_PATH", "./data/uploads"), AWSRegion: os.Getenv("AWS_REGION"), S3Bucket: os.Getenv("S3_BUCKET_NAME"), } if cfg.DBDriver != "postgres" && cfg.DBDriver != "sqlite" { return nil, fmt.Errorf("invalid DB_DRIVER %q (must be postgres or sqlite)", cfg.DBDriver) } if cfg.StorageDriver != "s3" && cfg.StorageDriver != "local" { return nil, fmt.Errorf("invalid STORAGE_DRIVER %q (must be s3 or local)", cfg.StorageDriver) } required := map[string]string{ "DATABASE_URL": cfg.DatabaseURL, "OIDC_ISSUER_URL": cfg.OIDCIssuerURL, "OIDC_CLIENT_ID": cfg.OIDCClientID, "OIDC_CLIENT_SECRET": cfg.OIDCClientSecret, "OIDC_REDIRECT_URL": cfg.OIDCRedirectURL, "PUBLIC_BASE_URL": cfg.PublicBaseURL, "JWT_SIGNING_SECRET": cfg.JWTSigningSecret, } // S3 credentials only matter when actually using S3. if cfg.StorageDriver == "s3" { required["AWS_REGION"] = cfg.AWSRegion required["S3_BUCKET_NAME"] = cfg.S3Bucket } for name, val := range required { if val == "" { return nil, fmt.Errorf("missing required env var %s", name) } } return cfg, nil } func getEnv(key, fallback string) string { if v := os.Getenv(key); v != "" { return v } return fallback }