Files
XHChat/server/internal/config/config.go
T

46 lines
1.2 KiB
Go

package config
import (
"errors"
"os"
"strings"
"time"
)
type Config struct {
Addr, DatabaseURL, RedisURL string
Origins map[string]bool
SessionTTL time.Duration
EnvelopeRetention time.Duration
}
func Load() (Config, error) {
c := Config{
Addr: os.Getenv("XHCHAT_ADDR"),
DatabaseURL: os.Getenv("DATABASE_URL"),
RedisURL: os.Getenv("REDIS_URL"),
Origins: make(map[string]bool),
SessionTTL: 12 * time.Hour,
EnvelopeRetention: 30 * 24 * time.Hour,
}
if c.Addr == "" {
c.Addr = "127.0.0.1:8080"
}
if raw := os.Getenv("XHCHAT_ENVELOPE_RETENTION"); raw != "" {
retention, err := time.ParseDuration(raw)
if err != nil || retention < time.Hour || retention > 365*24*time.Hour {
return Config{}, errors.New("XHCHAT_ENVELOPE_RETENTION must be between 1h and 8760h")
}
c.EnvelopeRetention = retention
}
for _, origin := range strings.Split(os.Getenv("XHCHAT_ORIGINS"), ",") {
if origin = strings.TrimSpace(origin); origin != "" {
c.Origins[origin] = true
}
}
if c.DatabaseURL == "" || c.RedisURL == "" || len(c.Origins) == 0 {
return Config{}, errors.New("DATABASE_URL, REDIS_URL and XHCHAT_ORIGINS are required")
}
return c, nil
}