Files
mtusi2026lab4-fork/backend/internal/config/config.go
T

169 lines
4.1 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
Host string
Port string
DatabaseURL string
SessionStore string
CacheURL string
SessionTTL time.Duration
IdempotencyTTL time.Duration
ShutdownDrain time.Duration
S3Endpoint string
S3Bucket string
S3UseSSL bool
S3Region string
S3AccessKey string
S3SecretKey string
}
func Load() (*Config, error) {
dsn, err := readSecret("DATABASE_URL_FILE", "DATABASE_URL")
if err != nil {
return nil, fmt.Errorf("database url: %w", err)
}
cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL")
if err != nil {
return nil, fmt.Errorf("cache url: %w", err)
}
ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600)
if err != nil {
return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err)
}
if ttlSeconds <= 0 {
return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным")
}
idemSeconds, err := readInt("IDEMPOTENCY_TTL_SECONDS", 86400)
if err != nil {
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS: %w", err)
}
if idemSeconds <= 0 {
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS должен быть положительным")
}
drainSeconds, err := readInt("SHUTDOWN_DRAIN_SECONDS", 3)
if err != nil {
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS: %w", err)
}
if drainSeconds < 0 {
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS не может быть отрицательным")
}
store := strings.ToLower(getenv("SESSION_STORE", "postgres"))
switch store {
case "postgres", "redis", "valkey":
default:
return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store)
}
s3Endpoint := getenv("S3_ENDPOINT", "")
if s3Endpoint == "" {
return nil, fmt.Errorf("S3_ENDPOINT не задан")
}
s3Bucket := getenv("S3_BUCKET", "")
if s3Bucket == "" {
return nil, fmt.Errorf("S3_BUCKET не задан")
}
s3UseSSL, err := readBool("S3_USE_SSL", false)
if err != nil {
return nil, fmt.Errorf("S3_USE_SSL: %w", err)
}
s3Region := getenv("S3_REGION", "us-east-1")
s3AccessKey, err := readSecret("S3_ACCESS_KEY_FILE", "S3_ACCESS_KEY")
if err != nil {
return nil, fmt.Errorf("s3 access key: %w", err)
}
s3SecretKey, err := readSecret("S3_SECRET_KEY_FILE", "S3_SECRET_KEY")
if err != nil {
return nil, fmt.Errorf("s3 secret key: %w", err)
}
return &Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
DatabaseURL: dsn,
SessionStore: store,
CacheURL: cacheURL,
SessionTTL: time.Duration(ttlSeconds) * time.Second,
IdempotencyTTL: time.Duration(idemSeconds) * time.Second,
ShutdownDrain: time.Duration(drainSeconds) * time.Second,
S3Endpoint: s3Endpoint,
S3Bucket: s3Bucket,
S3UseSSL: s3UseSSL,
S3Region: s3Region,
S3AccessKey: s3AccessKey,
S3SecretKey: s3SecretKey,
}, nil
}
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func readInt(key string, def int) (int, error) {
v := os.Getenv(key)
if v == "" {
return def, nil
}
n, err := strconv.Atoi(v)
if err != nil {
return 0, fmt.Errorf("не целое число: %q", v)
}
return n, nil
}
func readBool(key string, def bool) (bool, error) {
v := os.Getenv(key)
if v == "" {
return def, nil
}
b, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("не bool: %q", v)
}
return b, nil
}
func readSecret(fileEnv, envKey string) (string, error) {
v, err := readSecretOptional(fileEnv, envKey)
if err != nil {
return "", err
}
if v == "" {
return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey)
}
return v, nil
}
func readSecretOptional(fileEnv, envKey string) (string, error) {
if path := os.Getenv(fileEnv); path != "" {
b, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("чтение %s=%s: %w", fileEnv, path, err)
}
s := strings.TrimSpace(string(b))
if s == "" {
return "", fmt.Errorf("файл %s пуст", path)
}
return s, nil
}
return os.Getenv(envKey), nil
}