-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
61 lines (55 loc) · 1.42 KB
/
config.go
File metadata and controls
61 lines (55 loc) · 1.42 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
package main
import (
"log"
"os"
"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
}
func LoadConfig() Config {
return Config{
ListenAddr: env("LISTEN_ADDR", ":8080"),
SQLitePath: env("SQLITE_PATH", "/data/cacheppuccino.db"),
TranslationBaseURL: mustEnv("TRANSLATION_BASE_URL"),
TranslationApplicationID: mustEnv("TRANSLATION_APPLICATION_ID"),
TranslationAPIKey: mustEnv("TRANSLATION_API_KEY"),
HTTPTimeout: envDuration("HTTP_TIMEOUT", 30*time.Second),
PullInterval: envDuration("PULL_INTERVAL", 10*time.Minute),
InitialPullDeadline: envDuration("INITIAL_PULL_DEADLINE", 60*time.Second),
LogLevel: env("LOG_LEVEL", "info"),
}
}
func env(k, def string) string {
v := os.Getenv(k)
if v == "" {
return def
}
return v
}
func mustEnv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("missing required env: %s", k)
}
return v
}
func envDuration(k string, def time.Duration) time.Duration {
v := os.Getenv(k)
if v == "" {
return def
}
d, err := time.ParseDuration(v)
if err != nil {
return def
}
return d
}