forked from mtusi2026/mtusi2026lab4
284 lines
7.7 KiB
Go
284 lines
7.7 KiB
Go
// Package config loads and validates process configuration from environment
|
|
// variables and optional *_FILE secrets.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// Configuration may make a limit stricter, never looser.
|
|
MaxUploadBytesHardCap = 10 * 1024 * 1024
|
|
MaxImagePixelsHardCap = 40_000_000
|
|
|
|
// MaxJSONBodyBytes is the body budget of every JSON endpoint. It is also
|
|
// the slack between the global HTTP body limit and the upload limit, so a
|
|
// smaller MAX_UPLOAD_BYTES cannot strangle JSON requests.
|
|
MaxJSONBodyBytes = 1 * 1024 * 1024
|
|
)
|
|
|
|
type Config struct {
|
|
Host string
|
|
Port string
|
|
InstanceID string
|
|
SessionTTL time.Duration
|
|
IdempotencyTTL time.Duration
|
|
DatabaseURL string
|
|
CacheURL string
|
|
DBMaxConns int32
|
|
DBTimeout time.Duration
|
|
RedisTimeout time.Duration
|
|
StartupWait time.Duration
|
|
RunMigrations bool
|
|
MigrateOnly bool
|
|
|
|
S3Endpoint string
|
|
S3Region string
|
|
S3Bucket string
|
|
S3AccessKey string
|
|
S3SecretKey string
|
|
UploadTimeout time.Duration
|
|
DownloadTimeout time.Duration
|
|
|
|
MaxUploadBytes int64
|
|
MaxImagePixels int64
|
|
ImageDecodeBytes int64
|
|
UploadTempDir string
|
|
|
|
// HTTP timeouts and body limits are derived once in Load so that app.go
|
|
// and the handlers never invent values of their own.
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
IdleTimeout time.Duration
|
|
UploadDrainLimit int64
|
|
HTTPBodyLimit int
|
|
|
|
ReconcileGrace time.Duration
|
|
ReconcileInterval time.Duration
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
instance, err := instanceID()
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
sessionTTL, err := envInt("SESSION_TTL_SECONDS", 3600, 1, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
idempotencyTTL, err := envInt("IDEMPOTENCY_TTL_SECONDS", 86400, 1, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
maxConns, err := envInt("DB_POOL_MAX_CONNS", 10, 1, 100)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
maxUpload, err := envInt[int64]("MAX_UPLOAD_BYTES", MaxUploadBytesHardCap, 1, MaxUploadBytesHardCap)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
maxPixels, err := envInt[int64]("MAX_IMAGE_PIXELS", MaxImagePixelsHardCap, 1, MaxImagePixelsHardCap)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
decodeMB, err := envInt("IMAGE_DECODE_MEMORY_MB", 256, 32, 4096)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
cfg := Config{
|
|
Host: envOr("APP_HOST", "0.0.0.0"),
|
|
Port: envOr("APP_PORT", "8080"),
|
|
InstanceID: instance,
|
|
SessionTTL: time.Duration(sessionTTL) * time.Second,
|
|
IdempotencyTTL: time.Duration(idempotencyTTL) * time.Second,
|
|
DBMaxConns: int32(maxConns),
|
|
DBTimeout: 5 * time.Second,
|
|
RedisTimeout: 2 * time.Second,
|
|
StartupWait: 30 * time.Second,
|
|
S3Region: envOr("S3_REGION", "us-east-1"),
|
|
S3Bucket: envOr("S3_BUCKET", "messenger-attachments"),
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 60 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
UploadTimeout: 30 * time.Second,
|
|
DownloadTimeout: 60 * time.Second,
|
|
MaxUploadBytes: maxUpload,
|
|
MaxImagePixels: maxPixels,
|
|
ImageDecodeBytes: int64(decodeMB) * 1024 * 1024,
|
|
UploadTempDir: envOr("UPLOAD_TEMP_DIR", filepath.Join(os.TempDir(), "messenger-uploads")),
|
|
}
|
|
cfg.deriveHTTPLimits()
|
|
|
|
if cfg.RunMigrations, err = envBool("RUN_MIGRATIONS", true); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.MigrateOnly, err = envBool("MIGRATE_ONLY", false); err != nil {
|
|
return Config{}, err
|
|
}
|
|
reconcileOnly, err := envBool("RECONCILE_ONLY", false)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
reconcileGrace, err := envInt("RECONCILE_GRACE_SECONDS", 900, 60, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
cfg.ReconcileGrace = time.Duration(reconcileGrace) * time.Second
|
|
|
|
reconcileInterval, err := envInt("RECONCILE_INTERVAL_SECONDS", 300, 10, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
cfg.ReconcileInterval = time.Duration(reconcileInterval) * time.Second
|
|
|
|
if cfg.DatabaseURL, err = secret("DATABASE_URL"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.DatabaseURL == "" {
|
|
return Config{}, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required")
|
|
}
|
|
|
|
if cfg.MigrateOnly {
|
|
return cfg, nil
|
|
}
|
|
|
|
if cfg.S3Endpoint, err = secret("S3_ENDPOINT"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.S3Endpoint == "" {
|
|
return Config{}, fmt.Errorf("S3_ENDPOINT or S3_ENDPOINT_FILE is required")
|
|
}
|
|
if cfg.S3AccessKey, err = secret("S3_ACCESS_KEY_ID"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.S3AccessKey == "" {
|
|
return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID or S3_ACCESS_KEY_ID_FILE is required")
|
|
}
|
|
if cfg.S3SecretKey, err = secret("S3_SECRET_ACCESS_KEY"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.S3SecretKey == "" {
|
|
return Config{}, fmt.Errorf("S3_SECRET_ACCESS_KEY or S3_SECRET_ACCESS_KEY_FILE is required")
|
|
}
|
|
|
|
if reconcileOnly {
|
|
return cfg, nil
|
|
}
|
|
|
|
if cfg.CacheURL, err = secret("CACHE_URL"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.CacheURL == "" {
|
|
if cfg.CacheURL, err = secret("REDIS_URL"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
}
|
|
if cfg.CacheURL == "" {
|
|
return Config{}, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required")
|
|
}
|
|
|
|
if value := strings.ToLower(envOr("SESSION_STORE", "redis")); value != "redis" {
|
|
return Config{}, fmt.Errorf("SESSION_STORE must be redis (got %q)", value)
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// deriveHTTPLimits is the single place that relates the three HTTP budgets:
|
|
// the transport gate must admit both a full upload and a full JSON body, and it
|
|
// must leave room for the handler to drain an oversized declared body so the
|
|
// client receives a 413 instead of a connection reset.
|
|
func (c *Config) deriveHTTPLimits() {
|
|
c.UploadDrainLimit = c.MaxUploadBytes + MaxJSONBodyBytes
|
|
c.HTTPBodyLimit = int(c.UploadDrainLimit)
|
|
}
|
|
|
|
func instanceID() (string, error) {
|
|
if value := strings.TrimSpace(os.Getenv("INSTANCE_ID")); value != "" {
|
|
return value, nil
|
|
}
|
|
name, err := os.Hostname()
|
|
if err != nil {
|
|
return "", fmt.Errorf("determine INSTANCE_ID: %w", err)
|
|
}
|
|
if name = strings.TrimSpace(name); name == "" {
|
|
return "", fmt.Errorf("determine INSTANCE_ID: hostname is empty and INSTANCE_ID is not set")
|
|
}
|
|
return name, nil
|
|
}
|
|
|
|
func envBool(name string, fallback bool) (bool, error) {
|
|
value := os.Getenv(name)
|
|
if value == "" {
|
|
return fallback, nil
|
|
}
|
|
parsed, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
return false, fmt.Errorf("%s must be true or false", name)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
// envInt parses an integer setting into int or int64; max <= 0 means "no upper
|
|
// bound". Both widths are needed by Config, and one helper keeps validation
|
|
// identical for every numeric variable.
|
|
func envInt[T ~int | ~int64](name string, fallback, min, max T) (T, error) {
|
|
value := os.Getenv(name)
|
|
if value == "" {
|
|
return fallback, nil
|
|
}
|
|
|
|
parsed, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil || parsed < int64(min) || (max > 0 && parsed > int64(max)) {
|
|
if max > 0 {
|
|
return 0, fmt.Errorf("%s must be an integer between %d and %d", name, min, max)
|
|
}
|
|
return 0, fmt.Errorf("%s must be an integer >= %d", name, min)
|
|
}
|
|
result := T(parsed)
|
|
if int64(result) != parsed {
|
|
return 0, fmt.Errorf("%s is out of range", name)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func envOr(name, fallback string) string {
|
|
if value := os.Getenv(name); value != "" {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func secret(name string) (string, error) {
|
|
if value := os.Getenv(name); value != "" {
|
|
return value, nil
|
|
}
|
|
|
|
path := os.Getenv(name + "_FILE")
|
|
if path == "" {
|
|
return "", nil
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read %s_FILE: %w", name, err)
|
|
}
|
|
|
|
value := strings.TrimSpace(string(data))
|
|
if value == "" {
|
|
return "", fmt.Errorf("%s_FILE is empty", name)
|
|
}
|
|
|
|
return value, nil
|
|
}
|