Files
iknowyou/back/config/env/env.go
2026-04-06 15:12:34 +02:00

65 lines
1.4 KiB
Go

package env
import (
"fmt"
"os"
"strconv"
"time"
)
type Config struct {
Port int
ConfigPath string
FrontDir string // when set, serves the Astro static build at "/"
SearchTTL time.Duration
CleanupInterval time.Duration
Demo bool // when true, disables searches and config mutations
}
func Load() (*Config, error) {
cfg := &Config{
Port: 8080,
ConfigPath: "/etc/iky/config.yaml",
SearchTTL: 48 * time.Hour,
CleanupInterval: time.Hour,
}
if v := os.Getenv("IKY_PORT"); v != "" {
p, err := strconv.Atoi(v)
if err != nil || p < 1 || p > 65535 {
return nil, fmt.Errorf("env: IKY_PORT %q is not a valid port number", v)
}
cfg.Port = p
}
if v := os.Getenv("IKY_CONFIG"); v != "" {
cfg.ConfigPath = v
}
if v := os.Getenv("IKY_FRONT_DIR"); v != "" {
cfg.FrontDir = v
}
if v := os.Getenv("IKY_SEARCH_TTL"); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("env: IKY_SEARCH_TTL %q is not a valid duration", v)
}
cfg.SearchTTL = d
}
if v := os.Getenv("IKY_CLEANUP_INTERVAL"); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("env: IKY_CLEANUP_INTERVAL %q is not a valid duration", v)
}
cfg.CleanupInterval = d
}
if v := os.Getenv("IKY_DEMO"); v == "true" || v == "1" {
cfg.Demo = true
}
return cfg, nil
}