forked from mtusi2026/mtusi2026lab2
81 lines
2.9 KiB
Go
81 lines
2.9 KiB
Go
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/spf13/viper"
|
||
)
|
||
|
||
// Config приходит только из окружения (12-factor): в контейнер не копируются
|
||
// ни .env, ни файлы конфигурации. Значения по умолчанию — ниже.
|
||
type Config struct {
|
||
Host string `mapstructure:"APP_HOST"`
|
||
Port int `mapstructure:"APP_PORT"`
|
||
Env string `mapstructure:"APP_ENV"`
|
||
LogLevel string `mapstructure:"LOG_LEVEL"`
|
||
ShutdownTimeout time.Duration `mapstructure:"SHUTDOWN_TIMEOUT"`
|
||
MaxBodyBytes int64 `mapstructure:"MAX_BODY_BYTES"`
|
||
MaxInflight int `mapstructure:"MAX_INFLIGHT"`
|
||
MaxQueue int `mapstructure:"MAX_QUEUE"`
|
||
QueueTimeout time.Duration `mapstructure:"QUEUE_TIMEOUT"`
|
||
DatabaseURLFile string `mapstructure:"DATABASE_URL_FILE"`
|
||
CacheURLFile string `mapstructure:"CACHE_URL_FILE"`
|
||
SessionTTLSeconds int `mapstructure:"SESSION_TTL_SECONDS"`
|
||
MaxConns int32 `mapstructure:"PG_MAX_CONNS"`
|
||
}
|
||
|
||
var defaults = map[string]any{
|
||
"APP_HOST": "0.0.0.0",
|
||
"APP_PORT": 8080,
|
||
"APP_ENV": "lab",
|
||
"LOG_LEVEL": "info",
|
||
"SHUTDOWN_TIMEOUT": "8s",
|
||
"MAX_BODY_BYTES": 64 << 10,
|
||
"MAX_INFLIGHT": 4,
|
||
"MAX_QUEUE": 32,
|
||
"QUEUE_TIMEOUT": "1s",
|
||
"DATABASE_URL_FILE": "/run/secrets/database_url",
|
||
"CACHE_URL_FILE": "/run/secrets/cache_url",
|
||
"SESSION_TTL_SECONDS": 3600,
|
||
"PG_MAX_CONNS": 8,
|
||
}
|
||
|
||
func Load() (Config, error) {
|
||
v := viper.New()
|
||
for key, value := range defaults {
|
||
v.SetDefault(key, value)
|
||
}
|
||
v.AutomaticEnv()
|
||
var cfg Config
|
||
if err := v.Unmarshal(&cfg); err != nil {
|
||
return Config{}, fmt.Errorf("parse config: %w", err)
|
||
}
|
||
if cfg.ShutdownTimeout <= 0 || cfg.ShutdownTimeout > 9*time.Second {
|
||
return Config{}, fmt.Errorf("SHUTDOWN_TIMEOUT must be in (0, 9s]: docker stop ждёт 10 с")
|
||
}
|
||
if cfg.SessionTTLSeconds < 1 || cfg.SessionTTLSeconds > 86400 {
|
||
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be 1..86400")
|
||
}
|
||
if cfg.MaxConns<1||cfg.MaxConns>64{return Config{},fmt.Errorf("PG_MAX_CONNS must be 1..64")}
|
||
if cfg.MaxInflight<1||cfg.MaxInflight>4||cfg.MaxQueue<0||cfg.MaxQueue>128||cfg.QueueTimeout<=0||cfg.QueueTimeout>2*time.Second{return Config{},fmt.Errorf("HTTP gate exceeds the tested resource budget")}
|
||
if cfg.MaxBodyBytes<1||cfg.MaxBodyBytes>65536{return Config{},fmt.Errorf("MAX_BODY_BYTES must be 1..65536")}
|
||
return cfg, nil
|
||
}
|
||
|
||
func (c Config) Addr() string { return fmt.Sprintf("%s:%d", c.Host, c.Port) }
|
||
|
||
func ReadSecret(path string) (string, error) {
|
||
b, e := os.ReadFile(path)
|
||
if e != nil {
|
||
return "", fmt.Errorf("required secret file unavailable")
|
||
}
|
||
v := strings.TrimSpace(string(b))
|
||
if v == "" {
|
||
return "", fmt.Errorf("required secret is empty")
|
||
}
|
||
return v, nil
|
||
}
|