-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
55 lines (44 loc) · 1.08 KB
/
config.go
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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
)
type config struct {
BilderDir string `json:"bilder-dir"`
URLPathPrefix string `json:"url-path-prefix"`
AccessLog string `json:"access-log"`
Addr string `json:"addr"`
ReloadDelaySeconds int `json:"reload-delay-seconds"`
}
var defaultConfig = config{
BilderDir: "bilder",
Addr: "0.0.0.0:8173",
ReloadDelaySeconds: 10,
}
func mustParseConfig() config {
f := flag.String("config", "", "JSON config file for bilder.")
flag.Parse()
if *f == "" {
return defaultConfig
}
byts, err := ioutil.ReadFile(*f)
if err != nil {
log.Fatalf("Failed to read config file %#v err=%v", *f, err)
}
var c config
if err := json.Unmarshal(byts, &c); err != nil {
log.Fatalf("Failed to unmarshal contents of %#v as config, err=%v", *f, err)
}
if c.BilderDir == "" {
c.BilderDir = defaultConfig.BilderDir
}
if c.Addr == "" {
c.Addr = defaultConfig.Addr
}
if c.ReloadDelaySeconds == 0 {
c.ReloadDelaySeconds = defaultConfig.ReloadDelaySeconds
}
return c
}