feat:lab2-catchup

This commit is contained in:
Оля Мороз
2026-10-20 16:47:37 +03:00
parent 0ab3a341aa
commit ad9f6e8944
17 changed files with 1472 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
package config
import (
"os"
"strconv"
"time"
)
type Config struct {
Host string
Port int
Env string
DatabaseURL string
CacheURL string
SessionTTL time.Duration
SessionStore string // "postgres" | "redis"
RedisRequired bool
}
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func readSecretFromFile(path string) string {
if path == "" {
return ""
}
b, err := os.ReadFile(path)
if err != nil {
return ""
}
return string(b)
}
func Load() Config {
port := 8080
if v := os.Getenv("APP_PORT"); v != "" {
if p, err := strconv.Atoi(v); err == nil {
port = p
}
}
ttl := 3600
if v := os.Getenv("SESSION_TTL_SECONDS"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
ttl = n
}
}
// DATABASE_URL и CACHE_URL могут прийти либо напрямую, либо через *_FILE.
dbURL := os.Getenv("DATABASE_URL")
if dbURL == "" {
dbURL = readSecretFromFile(os.Getenv("DATABASE_URL_FILE"))
}
cacheURL := os.Getenv("CACHE_URL")
if cacheURL == "" {
cacheURL = readSecretFromFile(os.Getenv("CACHE_URL_FILE"))
}
store := getenv("SESSION_STORE", "postgres")
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: port,
Env: getenv("APP_ENV", "lab"),
DatabaseURL: dbURL,
CacheURL: cacheURL,
SessionTTL: time.Duration(ttl) * time.Second,
SessionStore: store,
RedisRequired: store == "redis",
}
}