-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
91 lines (80 loc) · 2.54 KB
/
Copy pathconfig.go
File metadata and controls
91 lines (80 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"errors"
"fmt"
"os"
"slices"
"time"
)
type Config struct {
ListenAddr string
SQLitePath string
TranslationBaseURL string
TranslationApplicationID string
TranslationAPIKey string
HTTPTimeout time.Duration
PullInterval time.Duration
InitialPullDeadline time.Duration
LogLevel string
}
var validLogLevels = []string{"debug", "info", "warn", "error"}
// LoadConfig reads configuration from the environment.
// It collects every problem instead of stopping at the first one,
// so a misconfigured deployment reports all mistakes at once.
func LoadConfig() (Config, error) {
var errs []error
requireEnv := func(k string) string {
v := os.Getenv(k)
if v == "" {
errs = append(errs, fmt.Errorf("missing required env: %s", k))
}
return v
}
envDuration := func(k string, def time.Duration) time.Duration {
v := os.Getenv(k)
if v == "" {
return def
}
d, err := time.ParseDuration(v)
if err != nil {
errs = append(errs, fmt.Errorf("invalid duration for %s: %q", k, v))
return def
}
return d
}
cfg := Config{
ListenAddr: env("LISTEN_ADDR", ":8080"),
SQLitePath: env("SQLITE_PATH", "/data/cacheppuccino.db"),
TranslationBaseURL: requireEnv("TRANSLATION_BASE_URL"),
TranslationApplicationID: requireEnv("TRANSLATION_APPLICATION_ID"),
TranslationAPIKey: requireEnv("TRANSLATION_API_KEY"),
HTTPTimeout: envDuration("HTTP_TIMEOUT", 30*time.Second),
PullInterval: envDuration("PULL_INTERVAL", 10*time.Minute),
InitialPullDeadline: envDuration("INITIAL_PULL_DEADLINE", 45*time.Second),
LogLevel: env("LOG_LEVEL", "info"),
}
if cfg.HTTPTimeout <= 0 {
errs = append(errs, fmt.Errorf("HTTP_TIMEOUT must be positive, got %s", cfg.HTTPTimeout))
}
if cfg.PullInterval <= 0 {
errs = append(errs, fmt.Errorf("PULL_INTERVAL must be positive, got %s", cfg.PullInterval))
}
// Zero means "no deadline" for the initial pull; only negatives are invalid.
if cfg.InitialPullDeadline < 0 {
errs = append(errs, fmt.Errorf("INITIAL_PULL_DEADLINE must not be negative, got %s", cfg.InitialPullDeadline))
}
if !slices.Contains(validLogLevels, cfg.LogLevel) {
errs = append(errs, fmt.Errorf("invalid LOG_LEVEL: %q (valid: debug, info, warn, error)", cfg.LogLevel))
}
if len(errs) > 0 {
return Config{}, errors.Join(errs...)
}
return cfg, nil
}
func env(k, def string) string {
v := os.Getenv(k)
if v == "" {
return def
}
return v
}