config.go75 lines, 3.0 KB
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/fiston-user/payments/internal/platform/config" |
| 10 | "github.com/fiston-user/payments/internal/platform/logging" |
| 11 | ) |
| 12 | |
| 13 | // Config is everything the gateway reads from its environment. |
| 14 | type Config struct { |
| 15 | Env string // development or production |
| 16 | HTTPAddr string |
| 17 | MetricsAddr string // internal only: never exposed to the internet |
| 18 | ShutdownTimeout time.Duration |
| 19 | DrainDelay time.Duration // how long readiness fails before we stop |
| 20 | LogLevel slog.Level |
| 21 | LogFormat string |
| 22 | WalletAddr string |
| 23 | RedisURL string |
| 24 | PublicKeyFile string |
| 25 | IPLimit int |
| 26 | UserLimit int |
| 27 | Origins []string |
| 28 | AuthURL *url.URL |
| 29 | TransferURL *url.URL |
| 30 | WebhookURL *url.URL |
| 31 | WalletURL *url.URL // the wallet's REST API, next to its gRPC one |
| 32 | TLSDir string // our certificate and key, and our CA's: for mTLS |
| 33 | InternalAddr string // the profiler, over mTLS |
| 34 | } |
| 35 | |
| 36 | // loadConfig reads the configuration. Everything has a sensible default except |
| 37 | // secrets, and every missing or invalid variable is reported at once. |
| 38 | func loadConfig(env *config.Env) (Config, error) { |
| 39 | c := Config{ |
| 40 | Env: env.OneOf("APP_ENV", "development", "development", "production"), |
| 41 | HTTPAddr: env.String("GATEWAY_HTTP_ADDR", ":8080"), |
| 42 | MetricsAddr: env.String("GATEWAY_METRICS_ADDR", ":9080"), |
| 43 | ShutdownTimeout: env.Duration("GATEWAY_SHUTDOWN_TIMEOUT", 10*time.Second), |
| 44 | DrainDelay: env.Duration("GATEWAY_DRAIN_DELAY", 0), |
| 45 | LogLevel: config.Parse(env, "LOG_LEVEL", slog.LevelInfo, logging.ParseLevel), |
| 46 | WalletAddr: env.String("GATEWAY_WALLET_ADDR", "dns:///localhost:9081"), |
| 47 | RedisURL: env.String("GATEWAY_REDIS_URL", "redis://localhost:6379/0"), |
| 48 | PublicKeyFile: env.Required("GATEWAY_AUTH_PUBLIC_KEY_FILE"), |
| 49 | IPLimit: env.Int("GATEWAY_IP_LIMIT", 20), |
| 50 | UserLimit: env.Int("GATEWAY_USER_LIMIT", 120), |
| 51 | Origins: strings.Split(env.String("GATEWAY_CORS_ORIGINS", "http://localhost:5173"), ","), |
| 52 | AuthURL: config.Parse(env, "GATEWAY_AUTH_URL", |
| 53 | &url.URL{Scheme: "http", Host: "localhost:8082"}, url.Parse), |
| 54 | TransferURL: config.Parse(env, "GATEWAY_TRANSFER_URL", |
| 55 | &url.URL{Scheme: "http", Host: "localhost:8083"}, url.Parse), |
| 56 | WebhookURL: config.Parse(env, "GATEWAY_WEBHOOK_URL", |
| 57 | &url.URL{Scheme: "http", Host: "localhost:8085"}, url.Parse), |
| 58 | WalletURL: config.Parse(env, "GATEWAY_WALLET_URL", |
| 59 | &url.URL{Scheme: "http", Host: "localhost:8081"}, url.Parse), |
| 60 | TLSDir: env.String("GATEWAY_TLS_DIR", "secrets/tls/gateway"), |
| 61 | InternalAddr: env.String("GATEWAY_INTERNAL_ADDR", ":9088"), |
| 62 | } |
| 63 | |
| 64 | // Text logs for humans in development, JSON for log collectors in production. |
| 65 | format := "text" |
| 66 | if c.Env == "production" { |
| 67 | format = "json" |
| 68 | } |
| 69 | c.LogFormat = env.OneOf("LOG_FORMAT", format, "text", "json") |
| 70 | |
| 71 | if c.IPLimit < 1 || c.UserLimit < 1 { |
| 72 | env.Invalid("GATEWAY_IP_LIMIT, GATEWAY_USER_LIMIT", "must be at least 1 a minute") |
| 73 | } |
| 74 | return c, env.Err() |
| 75 | } |