config.go83 lines, 3.5 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 wallet service reads from its environment. |
| 12 | type Config struct { |
| 13 | Env string // development or production |
| 14 | HTTPAddr string |
| 15 | GRPCAddr string |
| 16 | ShutdownTimeout time.Duration |
| 17 | DrainDelay time.Duration // how long readiness fails before we stop |
| 18 | LogLevel slog.Level |
| 19 | LogFormat string |
| 20 | DatabaseURL string |
| 21 | DBMaxConns int |
| 22 | DBReportConns int // a pool of their own for reports: statements, trial balances |
| 23 | RedisURL string |
| 24 | PublicKeyFile string |
| 25 | NATSURL string |
| 26 | NATSReplicas int // copies of the WALLET stream: 1 locally, 3 in production |
| 27 | FXQuoteTTL time.Duration // how long a quote's price holds |
| 28 | FXFeeBps int // our fee on conversions, in basis points |
| 29 | ReconcileEvery time.Duration // how often the ledger is reconciled |
| 30 | ReconcileAt time.Duration // how far into each period: 2h, with 24h, is 02:00 UTC |
| 31 | TLSDir string // our certificate and key, and our CA's: for mTLS |
| 32 | InternalAddr string // the internal routes, over mTLS |
| 33 | } |
| 34 | |
| 35 | // loadConfig reads the configuration. Everything has a sensible default except |
| 36 | // secrets, and every missing or invalid variable is reported at once. |
| 37 | func loadConfig(env *config.Env) (Config, error) { |
| 38 | c := Config{ |
| 39 | Env: env.OneOf("APP_ENV", "development", "development", "production"), |
| 40 | HTTPAddr: env.String("WALLET_HTTP_ADDR", ":8081"), |
| 41 | GRPCAddr: env.String("WALLET_GRPC_ADDR", ":9081"), |
| 42 | ShutdownTimeout: env.Duration("WALLET_SHUTDOWN_TIMEOUT", 10*time.Second), |
| 43 | DrainDelay: env.Duration("WALLET_DRAIN_DELAY", 0), |
| 44 | LogLevel: config.Parse(env, "LOG_LEVEL", slog.LevelInfo, logging.ParseLevel), |
| 45 | DatabaseURL: env.Required("WALLET_DATABASE_URL"), |
| 46 | DBMaxConns: env.Int("WALLET_DB_MAX_CONNS", 10), |
| 47 | DBReportConns: env.Int("WALLET_DB_REPORT_CONNS", 2), |
| 48 | RedisURL: env.String("WALLET_REDIS_URL", "redis://localhost:6379/0"), |
| 49 | PublicKeyFile: env.Required("WALLET_AUTH_PUBLIC_KEY_FILE"), |
| 50 | NATSURL: env.String("WALLET_NATS_URL", "nats://localhost:4222"), |
| 51 | NATSReplicas: env.Int("WALLET_NATS_REPLICAS", 1), |
| 52 | FXQuoteTTL: env.Duration("WALLET_FX_QUOTE_TTL", 30*time.Second), |
| 53 | FXFeeBps: env.Int("WALLET_FX_FEE_BPS", 50), |
| 54 | ReconcileEvery: env.Duration("WALLET_RECONCILE_EVERY", 24*time.Hour), |
| 55 | ReconcileAt: env.Duration("WALLET_RECONCILE_AT", 2*time.Hour), |
| 56 | TLSDir: env.String("WALLET_TLS_DIR", "secrets/tls/wallet"), |
| 57 | InternalAddr: env.String("WALLET_INTERNAL_ADDR", ":9091"), |
| 58 | } |
| 59 | |
| 60 | // Text logs for humans in development, JSON for log collectors in production. |
| 61 | format := "text" |
| 62 | if c.Env == "production" { |
| 63 | format = "json" |
| 64 | } |
| 65 | c.LogFormat = env.OneOf("LOG_FORMAT", format, "text", "json") |
| 66 | |
| 67 | if c.DBMaxConns < 1 || c.DBMaxConns > 100 { |
| 68 | env.Invalid("WALLET_DB_MAX_CONNS", "must be between 1 and 100") |
| 69 | } |
| 70 | if c.DBReportConns < 1 || c.DBReportConns > c.DBMaxConns { |
| 71 | env.Invalid("WALLET_DB_REPORT_CONNS", "must be between 1 and WALLET_DB_MAX_CONNS") |
| 72 | } |
| 73 | if c.NATSReplicas < 1 || c.NATSReplicas > 5 { |
| 74 | env.Invalid("WALLET_NATS_REPLICAS", "must be between 1 and 5") |
| 75 | } |
| 76 | if c.FXFeeBps < 0 || c.FXFeeBps > 1000 { |
| 77 | env.Invalid("WALLET_FX_FEE_BPS", "must be between 0 and 1000: ten percent") |
| 78 | } |
| 79 | if c.ReconcileEvery < time.Second { |
| 80 | env.Invalid("WALLET_RECONCILE_EVERY", "must be at least 1s") |
| 81 | } |
| 82 | return c, env.Err() |
| 83 | } |