forked from mtusi2026/mtusi2026lab3
feat:lab2-catchup
This commit is contained in:
@@ -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",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user