config.go80 lines, 3.4 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 | "github.com/fiston-user/payments/internal/transfer/risk" |
| 10 | ) |
| 11 | |
| 12 | // Config is everything the transfer service reads from its environment. |
| 13 | type Config struct { |
| 14 | Env string // development or production |
| 15 | HTTPAddr 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 | WalletAddr string // the wallet's gRPC API |
| 23 | PublicKeyFile string |
| 24 | NATSURL string |
| 25 | NATSReplicas int // copies of the TRANSFER stream: 1 locally, 3 in production |
| 26 | RedisURL string // the risk checks' velocity counters |
| 27 | ReviewTimeout time.Duration // then a transfer in review is rejected |
| 28 | Risk risk.Limits |
| 29 | TLSDir string // our certificate and key, and our CA's: for mTLS |
| 30 | InternalAddr string // the review API, over mTLS |
| 31 | } |
| 32 | |
| 33 | // loadConfig reads the configuration. Everything has a sensible default except |
| 34 | // secrets, and every missing or invalid variable is reported at once. |
| 35 | func loadConfig(env *config.Env) (Config, error) { |
| 36 | c := Config{ |
| 37 | Env: env.OneOf("APP_ENV", "development", "development", "production"), |
| 38 | HTTPAddr: env.String("TRANSFER_HTTP_ADDR", ":8083"), |
| 39 | ShutdownTimeout: env.Duration("TRANSFER_SHUTDOWN_TIMEOUT", 10*time.Second), |
| 40 | DrainDelay: env.Duration("TRANSFER_DRAIN_DELAY", 0), |
| 41 | LogLevel: config.Parse(env, "LOG_LEVEL", slog.LevelInfo, logging.ParseLevel), |
| 42 | DatabaseURL: env.Required("TRANSFER_DATABASE_URL"), |
| 43 | DBMaxConns: env.Int("TRANSFER_DB_MAX_CONNS", 10), |
| 44 | WalletAddr: env.String("TRANSFER_WALLET_ADDR", "dns:///localhost:9081"), |
| 45 | PublicKeyFile: env.Required("TRANSFER_AUTH_PUBLIC_KEY_FILE"), |
| 46 | NATSURL: env.String("TRANSFER_NATS_URL", "nats://localhost:4222"), |
| 47 | NATSReplicas: env.Int("TRANSFER_NATS_REPLICAS", 1), |
| 48 | RedisURL: env.String("TRANSFER_REDIS_URL", "redis://localhost:6379/0"), |
| 49 | ReviewTimeout: env.Duration("TRANSFER_REVIEW_TIMEOUT", 24*time.Hour), |
| 50 | Risk: risk.Limits{ |
| 51 | MaxCount: env.Int("TRANSFER_RISK_MAX_COUNT", 5), |
| 52 | CountWindow: env.Duration("TRANSFER_RISK_COUNT_WINDOW", 10*time.Minute), |
| 53 | MaxAmount: int64(env.Int("TRANSFER_RISK_MAX_AMOUNT", 200_000)), |
| 54 | AmountWindow: env.Duration("TRANSFER_RISK_AMOUNT_WINDOW", 24*time.Hour), |
| 55 | ReviewAbove: int64(env.Int("TRANSFER_RISK_REVIEW_ABOVE", 100_000)), |
| 56 | FirstTimeAbove: int64(env.Int("TRANSFER_RISK_FIRST_TIME_ABOVE", 25_000)), |
| 57 | NewWalletAge: env.Duration("TRANSFER_RISK_NEW_WALLET_AGE", 24*time.Hour), |
| 58 | }, |
| 59 | TLSDir: env.String("TRANSFER_TLS_DIR", "secrets/tls/transfer"), |
| 60 | InternalAddr: env.String("TRANSFER_INTERNAL_ADDR", ":9083"), |
| 61 | } |
| 62 | |
| 63 | // Text logs for humans in development, JSON for log collectors in production. |
| 64 | format := "text" |
| 65 | if c.Env == "production" { |
| 66 | format = "json" |
| 67 | } |
| 68 | c.LogFormat = env.OneOf("LOG_FORMAT", format, "text", "json") |
| 69 | |
| 70 | if c.DBMaxConns < 1 || c.DBMaxConns > 100 { |
| 71 | env.Invalid("TRANSFER_DB_MAX_CONNS", "must be between 1 and 100") |
| 72 | } |
| 73 | if c.NATSReplicas < 1 || c.NATSReplicas > 5 { |
| 74 | env.Invalid("TRANSFER_NATS_REPLICAS", "must be between 1 and 5") |
| 75 | } |
| 76 | if c.Risk.CountWindow > c.Risk.AmountWindow { |
| 77 | env.Invalid("TRANSFER_RISK_COUNT_WINDOW", "must not be longer than TRANSFER_RISK_AMOUNT_WINDOW") |
| 78 | } |
| 79 | return c, env.Err() |
| 80 | } |