forked from mtusi2026/mtusi2026lab3
161 lines
3.7 KiB
Go
161 lines
3.7 KiB
Go
// Package config loads and validates process configuration from environment
|
|
// variables and optional *_FILE secrets.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
instance, err := instanceID()
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
cfg := Config{
|
|
Host: envOr("APP_HOST", "0.0.0.0"),
|
|
Port: envOr("APP_PORT", "8080"),
|
|
InstanceID: instance,
|
|
DBMaxConns: 10,
|
|
DBTimeout: 5 * time.Second,
|
|
RedisTimeout: 2 * time.Second,
|
|
StartupWait: 30 * time.Second,
|
|
RunMigrations: true,
|
|
}
|
|
|
|
ttlSeconds, err := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600"))
|
|
if err != nil || ttlSeconds < 1 {
|
|
return cfg, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
|
|
}
|
|
cfg.SessionTTL = time.Duration(ttlSeconds) * time.Second
|
|
|
|
idempotencySeconds, err := strconv.Atoi(envOr("IDEMPOTENCY_TTL_SECONDS", "86400"))
|
|
if err != nil || idempotencySeconds < 1 {
|
|
return cfg, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS must be a positive integer")
|
|
}
|
|
cfg.IdempotencyTTL = time.Duration(idempotencySeconds) * time.Second
|
|
|
|
if cfg.RunMigrations, err = envBool("RUN_MIGRATIONS", true); err != nil {
|
|
return cfg, err
|
|
}
|
|
if cfg.MigrateOnly, err = envBool("MIGRATE_ONLY", false); err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
if value := envOr("DB_POOL_MAX_CONNS", ""); value != "" {
|
|
n, convErr := strconv.Atoi(value)
|
|
if convErr != nil || n < 1 || n > 100 {
|
|
return cfg, fmt.Errorf("DB_POOL_MAX_CONNS must be an integer between 1 and 100")
|
|
}
|
|
cfg.DBMaxConns = int32(n)
|
|
}
|
|
|
|
cfg.DatabaseURL, err = secret("DATABASE_URL")
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
if cfg.DatabaseURL == "" {
|
|
return cfg, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required")
|
|
}
|
|
|
|
if cfg.MigrateOnly {
|
|
return cfg, nil
|
|
}
|
|
|
|
cfg.CacheURL, err = secret("CACHE_URL")
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
if cfg.CacheURL == "" {
|
|
cfg.CacheURL, err = secret("REDIS_URL")
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
}
|
|
if cfg.CacheURL == "" {
|
|
return cfg, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required")
|
|
}
|
|
|
|
if value := strings.ToLower(envOr("SESSION_STORE", "redis")); value != "redis" {
|
|
return cfg, fmt.Errorf("SESSION_STORE must be redis (got %q)", value)
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|