forked from mtusi2026/mtusi2026lab5
216 lines
6.9 KiB
Go
216 lines
6.9 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var instanceIDPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
|
|
|
type Config struct {
|
|
Env string
|
|
Version string
|
|
InstanceID string
|
|
|
|
HTTP HTTP
|
|
Logger Logger
|
|
Limits Limits
|
|
Database Database
|
|
Cache Cache
|
|
Session Session
|
|
|
|
S3 S3
|
|
Attachments Attachments
|
|
Janitor Janitor
|
|
|
|
Idempotency Idempotency
|
|
HistoryCache HistoryCache
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
cfg := defaults()
|
|
if err := cfg.applyEnv(os.LookupEnv); err != nil {
|
|
return nil, fmt.Errorf("config: env: %w", err)
|
|
}
|
|
if cfg.InstanceID == "" {
|
|
cfg.InstanceID = defaultInstanceID()
|
|
}
|
|
if err := cfg.validate(); err != nil {
|
|
return nil, fmt.Errorf("config: validate: %w", err)
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func defaults() *Config {
|
|
return &Config{
|
|
Env: "lab",
|
|
Version: "dev",
|
|
HTTP: HTTP{
|
|
Addr: "0.0.0.0:8080",
|
|
ReadTimeoutMs: 10000,
|
|
WriteTimeoutMs: 10000,
|
|
IdleTimeoutMs: 60000,
|
|
ShutdownTimeoutMs: 10000,
|
|
ShutdownDrainMs: 3000,
|
|
},
|
|
Logger: Logger{Level: "info"},
|
|
Limits: Limits{
|
|
MaxBodyBytes: 64 * 1024,
|
|
MaxInFlight: 64,
|
|
MaxMessagesGlobal: 100000,
|
|
MaxHeapBytes: 256 * 1024 * 1024,
|
|
},
|
|
Database: Database{
|
|
MaxConns: 10,
|
|
ConnectTimeoutMs: 5000,
|
|
QueryTimeoutMs: 3000,
|
|
},
|
|
Cache: Cache{
|
|
PoolSize: 20,
|
|
TimeoutMs: 1000,
|
|
},
|
|
Session: Session{Store: "valkey", TTLSeconds: 3600},
|
|
S3: S3{
|
|
Endpoint: "http://s3:9000",
|
|
Region: "us-east-1",
|
|
Bucket: "messenger-attachments",
|
|
TimeoutMs: 8000,
|
|
},
|
|
Attachments: Attachments{
|
|
MaxUploadBytes: ContractMaxUploadBytes,
|
|
MaxImagePixels: ContractMaxImagePixels,
|
|
MaxConcurrentUploads: 16,
|
|
},
|
|
Janitor: Janitor{GraceSeconds: 3600, IntervalSeconds: 600, MaxDeletes: 1000},
|
|
Idempotency: Idempotency{TTLSeconds: MinIdempotencyTTLSeconds},
|
|
HistoryCache: HistoryCache{
|
|
Mode: "off",
|
|
PageTTLSeconds: 30,
|
|
TimeoutMs: 200,
|
|
PoolSize: 20,
|
|
},
|
|
}
|
|
}
|
|
|
|
// defaultInstanceID — hostname: в Docker это короткий ID контейнера, поэтому ответ
|
|
// можно сопоставить с конкретным контейнером. Значение неизменно на всю жизнь процесса.
|
|
func defaultInstanceID() string {
|
|
if h, err := os.Hostname(); err == nil && instanceIDPattern.MatchString(h) {
|
|
return h
|
|
}
|
|
return uuid.NewString()
|
|
}
|
|
|
|
func (c *Config) validate() error {
|
|
if !instanceIDPattern.MatchString(c.InstanceID) {
|
|
return fmt.Errorf("APP_INSTANCE_ID must match %s", instanceIDPattern)
|
|
}
|
|
if strings.TrimSpace(c.Env) == "" {
|
|
return errors.New("env is required")
|
|
}
|
|
if strings.TrimSpace(c.Version) == "" {
|
|
return errors.New("version is required")
|
|
}
|
|
if strings.TrimSpace(c.HTTP.Addr) == "" {
|
|
return errors.New("http.addr is required")
|
|
}
|
|
if c.HTTP.ShutdownTimeoutMs <= 0 || c.HTTP.ShutdownTimeoutMs > 10000 {
|
|
return fmt.Errorf("APP_SHUTDOWN_TIMEOUT_MS must be in (0, 10000], got %d", c.HTTP.ShutdownTimeoutMs)
|
|
}
|
|
if c.HTTP.ShutdownDrainMs < 0 || c.HTTP.ShutdownDrainMs > 10000 {
|
|
return fmt.Errorf("APP_SHUTDOWN_DRAIN_MS must be in [0, 10000], got %d", c.HTTP.ShutdownDrainMs)
|
|
}
|
|
if c.HTTP.ReadTimeoutMs <= 0 {
|
|
return fmt.Errorf("APP_READ_TIMEOUT_MS must be > 0, got %d", c.HTTP.ReadTimeoutMs)
|
|
}
|
|
if c.HTTP.WriteTimeoutMs <= 0 {
|
|
return fmt.Errorf("APP_WRITE_TIMEOUT_MS must be > 0, got %d", c.HTTP.WriteTimeoutMs)
|
|
}
|
|
if c.HTTP.IdleTimeoutMs <= 0 {
|
|
return fmt.Errorf("APP_IDLE_TIMEOUT_MS must be > 0, got %d", c.HTTP.IdleTimeoutMs)
|
|
}
|
|
|
|
if c.Limits.MaxBodyBytes <= 0 {
|
|
return fmt.Errorf("APP_MAX_BODY_BYTES must be > 0, got %d", c.Limits.MaxBodyBytes)
|
|
}
|
|
if c.Limits.MaxInFlight <= 0 {
|
|
return fmt.Errorf("APP_MAX_INFLIGHT must be > 0, got %d", c.Limits.MaxInFlight)
|
|
}
|
|
if c.Limits.MaxMessagesGlobal <= 0 {
|
|
return fmt.Errorf("APP_MAX_MESSAGES must be > 0, got %d", c.Limits.MaxMessagesGlobal)
|
|
}
|
|
if c.Limits.MaxHeapBytes == 0 {
|
|
return errors.New("APP_MAX_HEAP_BYTES must be > 0")
|
|
}
|
|
|
|
if c.S3.TimeoutMs <= 0 {
|
|
return fmt.Errorf("S3_TIMEOUT_MS must be > 0, got %d", c.S3.TimeoutMs)
|
|
}
|
|
if c.Attachments.MaxUploadBytes <= 0 || c.Attachments.MaxUploadBytes > ContractMaxUploadBytes {
|
|
return fmt.Errorf("MAX_UPLOAD_BYTES must be in (0, %d], got %d", ContractMaxUploadBytes, c.Attachments.MaxUploadBytes)
|
|
}
|
|
if c.Attachments.MaxImagePixels <= 0 || c.Attachments.MaxImagePixels > ContractMaxImagePixels {
|
|
return fmt.Errorf("MAX_IMAGE_PIXELS must be in (0, %d], got %d", ContractMaxImagePixels, c.Attachments.MaxImagePixels)
|
|
}
|
|
if c.Attachments.MaxConcurrentUploads <= 0 {
|
|
return fmt.Errorf("MAX_CONCURRENT_UPLOADS must be > 0, got %d", c.Attachments.MaxConcurrentUploads)
|
|
}
|
|
|
|
if c.Janitor.GraceSeconds < MinJanitorGraceSeconds {
|
|
return fmt.Errorf("JANITOR_GRACE_SECONDS must be >= %d, got %d", MinJanitorGraceSeconds, c.Janitor.GraceSeconds)
|
|
}
|
|
if c.Janitor.IntervalSeconds <= 0 {
|
|
return fmt.Errorf("JANITOR_INTERVAL_SECONDS must be > 0, got %d", c.Janitor.IntervalSeconds)
|
|
}
|
|
if c.Janitor.MaxDeletes <= 0 {
|
|
return fmt.Errorf("JANITOR_MAX_DELETES must be > 0, got %d", c.Janitor.MaxDeletes)
|
|
}
|
|
|
|
if c.Database.MaxConns <= 0 {
|
|
return fmt.Errorf("DB_MAX_CONNS must be > 0, got %d", c.Database.MaxConns)
|
|
}
|
|
if c.Database.ConnectTimeoutMs <= 0 {
|
|
return fmt.Errorf("DB_CONNECT_TIMEOUT_MS must be > 0, got %d", c.Database.ConnectTimeoutMs)
|
|
}
|
|
if c.Database.QueryTimeoutMs <= 0 {
|
|
return fmt.Errorf("DB_QUERY_TIMEOUT_MS must be > 0, got %d", c.Database.QueryTimeoutMs)
|
|
}
|
|
if c.Cache.PoolSize <= 0 {
|
|
return fmt.Errorf("CACHE_POOL_SIZE must be > 0, got %d", c.Cache.PoolSize)
|
|
}
|
|
if c.Cache.TimeoutMs <= 0 {
|
|
return fmt.Errorf("CACHE_TIMEOUT_MS must be > 0, got %d", c.Cache.TimeoutMs)
|
|
}
|
|
switch c.HistoryCache.Mode {
|
|
case "off", "naive", "safe":
|
|
default:
|
|
return fmt.Errorf("HISTORY_CACHE must be off|naive|safe, got %q", c.HistoryCache.Mode)
|
|
}
|
|
if c.HistoryCache.PageTTLSeconds <= 0 {
|
|
return fmt.Errorf("HISTORY_CACHE_TTL_SECONDS must be > 0, got %d", c.HistoryCache.PageTTLSeconds)
|
|
}
|
|
if c.HistoryCache.TimeoutMs <= 0 || c.HistoryCache.PoolSize <= 0 {
|
|
return errors.New("HISTORY_CACHE_TIMEOUT_MS and HISTORY_CACHE_POOL_SIZE must be > 0")
|
|
}
|
|
if c.HistoryCache.FillDelayMs < 0 || c.HistoryCache.FillDelayMs > 1000 {
|
|
return fmt.Errorf("HISTORY_CACHE_FILL_DELAY_MS must be in [0, 1000], got %d", c.HistoryCache.FillDelayMs)
|
|
}
|
|
// Реализовано одно хранилище сессий; иное значение — ошибка конфигурации, а не тихий выбор.
|
|
if c.Session.Store != "valkey" {
|
|
return fmt.Errorf("SESSION_STORE must be valkey (the only implemented session store), got %q", c.Session.Store)
|
|
}
|
|
if c.Idempotency.TTLSeconds < MinIdempotencyTTLSeconds {
|
|
return fmt.Errorf("IDEMPOTENCY_TTL_SECONDS must be >= %d, got %d", MinIdempotencyTTLSeconds, c.Idempotency.TTLSeconds)
|
|
}
|
|
if c.Session.TTLSeconds <= 0 {
|
|
return fmt.Errorf("SESSION_TTL_SECONDS must be > 0, got %d", c.Session.TTLSeconds)
|
|
}
|
|
|
|
return nil
|
|
}
|