forked from mtusi2026/mtusi2026lab2
147 lines
4.2 KiB
Go
147 lines
4.2 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
Env string
|
|
Version string
|
|
|
|
HTTP HTTP
|
|
Logger Logger
|
|
Limits Limits
|
|
Database Database
|
|
Cache Cache
|
|
Session Session
|
|
|
|
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 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,
|
|
},
|
|
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},
|
|
HistoryCache: HistoryCache{
|
|
Mode: "off",
|
|
PageTTLSeconds: 30,
|
|
TimeoutMs: 200,
|
|
PoolSize: 20,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Config) validate() error {
|
|
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.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.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.Session.TTLSeconds <= 0 {
|
|
return fmt.Errorf("SESSION_TTL_SECONDS must be > 0, got %d", c.Session.TTLSeconds)
|
|
}
|
|
|
|
return nil
|
|
}
|