forked from mtusi2026/mtusi2026lab5
396 lines
11 KiB
Go
396 lines
11 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"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/model"
|
|
)
|
|
|
|
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
|
|
|
|
// Image variant parameters. The validator and the worker must agree on the
|
|
// contract dimensions, and the experiment records these values, so they
|
|
// have exactly one definition.
|
|
ThumbnailMax = 256
|
|
CropSize = 128
|
|
OptimizedMax = 1280
|
|
JPEGQuality = 85
|
|
WebPQuality = 82
|
|
WebPMethod = 4
|
|
)
|
|
|
|
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
|
|
|
|
// BrokerURL is required by the API (outbox
|
|
// publisher) and the worker (consumer), but not by the reconciler.
|
|
BrokerURL string
|
|
WorkerOnly bool
|
|
JobQueue string
|
|
JobMaxAttempts int
|
|
JobTimeout time.Duration
|
|
JobLease time.Duration
|
|
RetryBase time.Duration
|
|
RetryMax time.Duration
|
|
WorkerConcurrency int
|
|
WorkerPrefetch int
|
|
MaxPendingJobs int
|
|
LeaseSweep time.Duration
|
|
StagingRetention time.Duration
|
|
WorkTempDir string
|
|
WorkerFaultMode string
|
|
CrashAfterEffect bool
|
|
CrashAfterClaim bool
|
|
}
|
|
|
|
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.WorkerOnly, err = envBool("WORKER_ONLY", false); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.CrashAfterEffect, err = envBool("WORKER_CRASH_AFTER_EFFECT", false); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.CrashAfterClaim, err = envBool("WORKER_CRASH_AFTER_CLAIM", false); err != nil {
|
|
return Config{}, err
|
|
}
|
|
jobAttempts, err := envInt("JOB_MAX_ATTEMPTS", 5, 1, 100)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
jobTimeout, err := envInt("JOB_TIMEOUT_SECONDS", 60, 5, 3600)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
jobLease, err := envInt("JOB_LEASE_SECONDS", 90, 10, 7200)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
retryBase, err := envInt("JOB_RETRY_BASE_SECONDS", 2, 1, 3600)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
retryMax, err := envInt("JOB_RETRY_MAX_SECONDS", 30, 1, 7200)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
concurrency, err := envInt("WORKER_CONCURRENCY", 2, 1, 128)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
prefetch, err := envInt("WORKER_PREFETCH", 2, 1, 512)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
maxPending, err := envInt("MAX_PENDING_JOBS", 1000, 0, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
leaseSweep, err := envInt("LEASE_SWEEP_SECONDS", 15, 1, 3600)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
stagingRetention, err := envInt("STAGING_RETENTION_SECONDS", 86400, 60, 0)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
cfg.JobQueue = envOr("JOB_QUEUE", model.JobTypeAttachmentProcessV1)
|
|
cfg.JobMaxAttempts = jobAttempts
|
|
cfg.JobTimeout = time.Duration(jobTimeout) * time.Second
|
|
cfg.JobLease = time.Duration(jobLease) * time.Second
|
|
cfg.RetryBase = time.Duration(retryBase) * time.Second
|
|
cfg.RetryMax = time.Duration(retryMax) * time.Second
|
|
cfg.WorkerConcurrency = concurrency
|
|
cfg.WorkerPrefetch = prefetch
|
|
cfg.MaxPendingJobs = maxPending
|
|
cfg.LeaseSweep = time.Duration(leaseSweep) * time.Second
|
|
cfg.StagingRetention = time.Duration(stagingRetention) * time.Second
|
|
cfg.WorkTempDir = envOr("WORK_TEMP_DIR", filepath.Join(os.TempDir(), "messenger-work"))
|
|
cfg.WorkerFaultMode = envOr("WORKER_FAULT_MODE", "none")
|
|
switch cfg.WorkerFaultMode {
|
|
case "none", "transient-once", "permanent-once":
|
|
default:
|
|
return Config{}, fmt.Errorf("WORKER_FAULT_MODE must be none, transient-once or permanent-once")
|
|
}
|
|
|
|
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.BrokerURL, err = secret("BROKER_URL"); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.BrokerURL == "" {
|
|
return Config{}, fmt.Errorf("BROKER_URL or BROKER_URL_FILE is required")
|
|
}
|
|
|
|
if cfg.WorkerOnly {
|
|
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
|
|
}
|