config.go55 lines, 2.1 KB
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/fiston-user/payments/internal/platform/config" |
| 8 | "github.com/fiston-user/payments/internal/platform/logging" |
| 9 | ) |
| 10 | |
| 11 | // Config is everything the notification service reads from its environment. |
| 12 | type Config struct { |
| 13 | Env string // development or production |
| 14 | HTTPAddr string |
| 15 | ShutdownTimeout time.Duration |
| 16 | DrainDelay time.Duration // how long readiness fails before we stop |
| 17 | LogLevel slog.Level |
| 18 | LogFormat string |
| 19 | DatabaseURL string |
| 20 | DBMaxConns int |
| 21 | NATSURL string |
| 22 | AuthURL string // the auth service, which knows email addresses |
| 23 | SMTPAddr string // where email goes: Mailpit, in development |
| 24 | TLSDir string // our certificate and key, and our CA's: for mTLS |
| 25 | } |
| 26 | |
| 27 | // loadConfig reads the configuration. Everything has a sensible default except |
| 28 | // secrets, and every missing or invalid variable is reported at once. |
| 29 | func loadConfig(env *config.Env) (Config, error) { |
| 30 | c := Config{ |
| 31 | Env: env.OneOf("APP_ENV", "development", "development", "production"), |
| 32 | HTTPAddr: env.String("NOTIFICATION_HTTP_ADDR", ":8084"), |
| 33 | ShutdownTimeout: env.Duration("NOTIFICATION_SHUTDOWN_TIMEOUT", 10*time.Second), |
| 34 | DrainDelay: env.Duration("NOTIFICATION_DRAIN_DELAY", 0), |
| 35 | LogLevel: config.Parse(env, "LOG_LEVEL", slog.LevelInfo, logging.ParseLevel), |
| 36 | DatabaseURL: env.Required("NOTIFICATION_DATABASE_URL"), |
| 37 | DBMaxConns: env.Int("NOTIFICATION_DB_MAX_CONNS", 10), |
| 38 | NATSURL: env.String("NOTIFICATION_NATS_URL", "nats://localhost:4222"), |
| 39 | AuthURL: env.String("NOTIFICATION_AUTH_URL", "https://localhost:9082"), |
| 40 | SMTPAddr: env.String("NOTIFICATION_SMTP_ADDR", "localhost:1025"), |
| 41 | TLSDir: env.String("NOTIFICATION_TLS_DIR", "secrets/tls/notification"), |
| 42 | } |
| 43 | |
| 44 | // Text logs for humans in development, JSON for log collectors in production. |
| 45 | format := "text" |
| 46 | if c.Env == "production" { |
| 47 | format = "json" |
| 48 | } |
| 49 | c.LogFormat = env.OneOf("LOG_FORMAT", format, "text", "json") |
| 50 | |
| 51 | if c.DBMaxConns < 1 || c.DBMaxConns > 100 { |
| 52 | env.Invalid("NOTIFICATION_DB_MAX_CONNS", "must be between 1 and 100") |
| 53 | } |
| 54 | return c, env.Err() |
| 55 | } |