forked from mtusi2026/mtusi2026lab2
feat: add PostgreSQL persistence and sessions
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Address string
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
ConnectTimeout time.Duration
|
||||
RequestTimeout time.Duration
|
||||
SwaggerPort string
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
host, err := requiredEnv("APP_HOST")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
port, err := requiredPort("APP_PORT")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
databaseURL, err := secretFromEnv("DATABASE_URL_FILE")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
cacheURL, err := secretFromEnv("CACHE_URL_FILE")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if store, err := requiredEnv("SESSION_STORE"); err != nil {
|
||||
return Config{}, err
|
||||
} else if store != "valkey" {
|
||||
return Config{}, errors.New("SESSION_STORE must be valkey")
|
||||
}
|
||||
ttlSeconds, err := requiredPositiveInt("SESSION_TTL_SECONDS")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConns, err := intEnv("DB_MAX_CONNS", 10, 1, 100)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
minConns, err := intEnv("DB_MIN_CONNS", 1, 0, maxConns)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
connectSeconds, err := intEnv("DEPENDENCY_TIMEOUT_SECONDS", 5, 1, 60)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
swaggerPort := os.Getenv("SWAGGER_PORT")
|
||||
if swaggerPort == "" {
|
||||
swaggerPort = "8081"
|
||||
}
|
||||
if _, err := parsePort(swaggerPort); err != nil {
|
||||
return Config{}, fmt.Errorf("SWAGGER_PORT: %w", err)
|
||||
}
|
||||
return Config{
|
||||
Address: net.JoinHostPort(host, port),
|
||||
DatabaseURL: databaseURL,
|
||||
CacheURL: cacheURL,
|
||||
SessionTTL: time.Duration(ttlSeconds) * time.Second,
|
||||
DBMaxConns: int32(maxConns),
|
||||
DBMinConns: int32(minConns),
|
||||
ConnectTimeout: time.Duration(connectSeconds) * time.Second,
|
||||
RequestTimeout: time.Duration(connectSeconds) * time.Second,
|
||||
SwaggerPort: swaggerPort,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func requiredEnv(name string) (string, error) {
|
||||
value := strings.TrimSpace(os.Getenv(name))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s is required", name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func requiredPort(name string) (string, error) {
|
||||
value, err := requiredEnv(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := parsePort(value); err != nil {
|
||||
return "", fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func parsePort(value string) (int, error) {
|
||||
port, err := strconv.Atoi(value)
|
||||
if err != nil || port < 1 || port > 65535 {
|
||||
return 0, errors.New("must be between 1 and 65535")
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
|
||||
func requiredPositiveInt(name string) (int, error) {
|
||||
value, err := requiredEnv(name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed <= 0 {
|
||||
return 0, fmt.Errorf("%s must be a positive integer", name)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func intEnv(name string, fallback, minimum, maximum int) (int, error) {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < minimum || parsed > maximum {
|
||||
return 0, fmt.Errorf("%s must be between %d and %d", name, minimum, maximum)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func secretFromEnv(name string) (string, error) {
|
||||
path, err := requiredEnv(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s: %w", name, err)
|
||||
}
|
||||
value := strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s points to an empty file", name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
Reference in New Issue
Block a user