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
|
package main
import (
"flag"
"github.com/BurntSushi/toml"
"log"
"os"
)
type conf struct {
System systemConf `toml:"system"`
Server serverConf `toml:"server"`
}
type serverConf struct {
Host string `toml:"host"`
Unix bool `toml:"unix"`
Port uint16 `toml:"port"`
Proxy bool `toml:"proxy"`
TrustedProxies []string `toml:"trusted_proxies"`
}
type systemConf struct {
Verbose bool `toml:"verbose"`
Backend string `toml:"backend"`
Store string `toml:"store"`
SingleUser bool `toml:"single-user"`
Private bool `toml:"private"`
Register bool `toml:"register"`
}
var (
config conf
confPath string
confRead = false
)
func init() {
flag.StringVar(&confPath, "c", "server.conf", "specify path to configuration file")
}
func confLoad() {
if confRead {
panic("configuration read called when already read")
}
defer func() { confRead = true }()
if meta, err := toml.DecodeFile(confPath, &config); err != nil {
if !os.IsNotExist(err) {
log.Fatalf("error parsing configuration: %s", err)
}
var file *os.File
if file, err = os.Create(confPath); err != nil {
log.Fatalf("error creating configuration file: %s", err)
} else if err = toml.NewEncoder(file).Encode(defConf); err != nil {
log.Fatalf("error generating default configuration: %s", err)
}
config = defConf
return
} else {
for _, key := range meta.Undecoded() {
log.Printf("unused key in configuration file: %s", key.String())
}
}
}
var defConf = conf{
System: systemConf{
Verbose: false,
Backend: "filesystem",
Store: "db",
SingleUser: true,
Private: false,
Register: false,
},
Server: serverConf{
Host: "127.0.0.1",
Unix: false,
Port: 7777,
Proxy: true,
TrustedProxies: []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
},
},
}
|