forked from mtusi2026/mtusi2026lab2
ЛР 2: PostgreSQL, сессии в Valkey, кэш истории (трек 1)
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
type Database struct {
|
||||
URLFile string
|
||||
URL string
|
||||
MaxConns int32
|
||||
ConnectTimeoutMs int
|
||||
QueryTimeoutMs int
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (c *Config) applyEnv(lookup func(string) (string, bool)) error {
|
||||
get := func(key string) (string, bool) {
|
||||
v, ok := lookup(key)
|
||||
v = strings.TrimSpace(v)
|
||||
return v, ok && v != ""
|
||||
}
|
||||
|
||||
if v, ok := get("APP_ENV"); ok {
|
||||
c.Env = v
|
||||
}
|
||||
if v, ok := get("APP_VERSION"); ok {
|
||||
c.Version = v
|
||||
}
|
||||
if v, ok := get("APP_LOG_LEVEL"); ok {
|
||||
c.Logger.Level = v
|
||||
}
|
||||
if v, ok := get("DATABASE_URL_FILE"); ok {
|
||||
c.Database.URLFile = v
|
||||
}
|
||||
if v, ok := get("DATABASE_URL"); ok {
|
||||
c.Database.URL = v
|
||||
}
|
||||
if v, ok := get("CACHE_URL_FILE"); ok {
|
||||
c.Cache.URLFile = v
|
||||
}
|
||||
if v, ok := get("CACHE_URL"); ok {
|
||||
c.Cache.URL = v
|
||||
}
|
||||
if v, ok := get("SESSION_STORE"); ok {
|
||||
c.Session.Store = strings.ToLower(v)
|
||||
}
|
||||
if v, ok := get("HISTORY_CACHE"); ok {
|
||||
c.HistoryCache.Mode = strings.ToLower(v)
|
||||
}
|
||||
if v, ok := get("HISTORY_CACHE_URL_FILE"); ok {
|
||||
c.HistoryCache.URLFile = v
|
||||
}
|
||||
if v, ok := get("HISTORY_CACHE_URL"); ok {
|
||||
c.HistoryCache.URL = v
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(c.HTTP.Addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("default addr %q: %w", c.HTTP.Addr, err)
|
||||
}
|
||||
if v, ok := get("APP_HOST"); ok {
|
||||
host = v
|
||||
}
|
||||
if v, ok := get("APP_PORT"); ok {
|
||||
port = v
|
||||
}
|
||||
if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 {
|
||||
return fmt.Errorf("APP_PORT must be in [1, 65535], got %q", port)
|
||||
}
|
||||
c.HTTP.Addr = net.JoinHostPort(host, port)
|
||||
|
||||
ints := []struct {
|
||||
key string
|
||||
dst *int
|
||||
}{
|
||||
{"APP_READ_TIMEOUT_MS", &c.HTTP.ReadTimeoutMs},
|
||||
{"APP_WRITE_TIMEOUT_MS", &c.HTTP.WriteTimeoutMs},
|
||||
{"APP_IDLE_TIMEOUT_MS", &c.HTTP.IdleTimeoutMs},
|
||||
{"APP_SHUTDOWN_TIMEOUT_MS", &c.HTTP.ShutdownTimeoutMs},
|
||||
{"APP_MAX_INFLIGHT", &c.Limits.MaxInFlight},
|
||||
{"APP_MAX_MESSAGES", &c.Limits.MaxMessagesGlobal},
|
||||
{"DB_CONNECT_TIMEOUT_MS", &c.Database.ConnectTimeoutMs},
|
||||
{"DB_QUERY_TIMEOUT_MS", &c.Database.QueryTimeoutMs},
|
||||
{"CACHE_POOL_SIZE", &c.Cache.PoolSize},
|
||||
{"CACHE_TIMEOUT_MS", &c.Cache.TimeoutMs},
|
||||
{"SESSION_TTL_SECONDS", &c.Session.TTLSeconds},
|
||||
{"HISTORY_CACHE_TTL_SECONDS", &c.HistoryCache.PageTTLSeconds},
|
||||
{"HISTORY_CACHE_TIMEOUT_MS", &c.HistoryCache.TimeoutMs},
|
||||
{"HISTORY_CACHE_POOL_SIZE", &c.HistoryCache.PoolSize},
|
||||
{"HISTORY_CACHE_FILL_DELAY_MS", &c.HistoryCache.FillDelayMs},
|
||||
}
|
||||
for _, it := range ints {
|
||||
v, ok := get(it.key)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: not an integer: %q", it.key, v)
|
||||
}
|
||||
*it.dst = n
|
||||
}
|
||||
|
||||
if v, ok := get("APP_MAX_BODY_BYTES"); ok {
|
||||
n, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("APP_MAX_BODY_BYTES: not an integer: %q", v)
|
||||
}
|
||||
c.Limits.MaxBodyBytes = n
|
||||
}
|
||||
if v, ok := get("DB_MAX_CONNS"); ok {
|
||||
n, err := strconv.ParseInt(v, 10, 32)
|
||||
if err != nil {
|
||||
return fmt.Errorf("DB_MAX_CONNS: not an integer: %q", v)
|
||||
}
|
||||
c.Database.MaxConns = int32(n)
|
||||
}
|
||||
if v, ok := get("APP_MAX_HEAP_BYTES"); ok {
|
||||
n, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("APP_MAX_HEAP_BYTES: not a non-negative integer: %q", v)
|
||||
}
|
||||
c.Limits.MaxHeapBytes = n
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLoad_Defaults(t *testing.T) {
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.HTTP.Addr != "0.0.0.0:8080" || cfg.Env != "lab" || cfg.Limits.MaxInFlight != 64 {
|
||||
t.Errorf("cfg = %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_EnvOverridesDefaults(t *testing.T) {
|
||||
t.Setenv("APP_HOST", "127.0.0.1")
|
||||
t.Setenv("APP_PORT", "9090")
|
||||
t.Setenv("APP_ENV", "docker")
|
||||
t.Setenv("APP_MAX_INFLIGHT", "8")
|
||||
t.Setenv("APP_MAX_BODY_BYTES", "2048")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.HTTP.Addr != "127.0.0.1:9090" {
|
||||
t.Errorf("addr = %q, want 127.0.0.1:9090", cfg.HTTP.Addr)
|
||||
}
|
||||
if cfg.Env != "docker" {
|
||||
t.Errorf("env = %q, want docker", cfg.Env)
|
||||
}
|
||||
if cfg.Limits.MaxInFlight != 8 || cfg.Limits.MaxBodyBytes != 2048 {
|
||||
t.Errorf("limits = %+v", cfg.Limits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_PortOnlyKeepsDefaultHost(t *testing.T) {
|
||||
t.Setenv("APP_PORT", "18080")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.HTTP.Addr != "0.0.0.0:18080" {
|
||||
t.Errorf("addr = %q, want 0.0.0.0:18080", cfg.HTTP.Addr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidEnvFails(t *testing.T) {
|
||||
for key, val := range map[string]string{
|
||||
"APP_PORT": "http",
|
||||
"APP_MAX_INFLIGHT": "many",
|
||||
"APP_MAX_MESSAGES": "0",
|
||||
"APP_SHUTDOWN_TIMEOUT_MS": "30000",
|
||||
} {
|
||||
t.Run(key, func(t *testing.T) {
|
||||
t.Setenv(key, val)
|
||||
if _, err := Load(); err == nil {
|
||||
t.Errorf("%s=%q: expected error", key, val)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
type HistoryCache struct {
|
||||
Mode string
|
||||
URLFile string
|
||||
URL string
|
||||
PageTTLSeconds int
|
||||
TimeoutMs int
|
||||
PoolSize int
|
||||
FillDelayMs int
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
type HTTP struct {
|
||||
Addr string
|
||||
ReadTimeoutMs int
|
||||
WriteTimeoutMs int
|
||||
IdleTimeoutMs int
|
||||
ShutdownTimeoutMs int
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
type Limits struct {
|
||||
MaxBodyBytes int64
|
||||
MaxInFlight int
|
||||
MaxMessagesGlobal int
|
||||
MaxHeapBytes uint64
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type Logger struct {
|
||||
Level string
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
type Session struct {
|
||||
Store string
|
||||
TTLSeconds int
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
URLFile string
|
||||
URL string
|
||||
PoolSize int
|
||||
TimeoutMs int
|
||||
}
|
||||
Reference in New Issue
Block a user