chore(solution): port lab3 sources

This commit is contained in:
2026-11-20 03:05:34 +03:00
parent ec540a1a72
commit 083d0d80cc
44 changed files with 2745 additions and 1 deletions
+60
View File
@@ -0,0 +1,60 @@
package config
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
}
func Load() (Config, error) {
ttl, err := strconv.Atoi(getenv("SESSION_TTL_SECONDS", "3600"))
if err != nil || ttl <= 0 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
}
databaseURL, err := readSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return Config{}, err
}
cacheURL, err := readSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
if err != nil {
return Config{}, err
}
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
}, nil
}
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
func readSecret(key, def string) (string, error) {
path := getenv(key, def)
b, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("%s: cannot read %s", key, path)
}
return strings.TrimSpace(string(b)), nil
}
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}