-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
55 lines (44 loc) · 1.21 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 (
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
)
type cfgResolver struct {
Type string `koanf:"type"`
Urls []string `koanf:"urls"`
}
type cfgCache struct {
Cache bool `koanf:"cache"`
MaxItems int `koanf:"max_items"`
}
type cfgLog struct {
LogLevel string `koanf:"log_level"`
LogQueries bool `koanf:"log_queries"`
}
type Config struct {
BindAddress string `koanf:"bind_address"`
BootstrapAddress string `koanf:"bootstrap_address"`
Cache cfgCache `koanf:"cache"`
Resolver cfgResolver `koanf:"resolver"`
Log cfgLog `koanf:"log"`
}
func initConfig(cfgPath string) (Config, error) {
k := koanf.New(".")
// Set default values
k.Load(confmap.Provider(map[string]interface{}{
"cache.cache": true,
"bind_address": "127.0.0.1:53",
"bootstrap_address": "9.9.9.9:53",
"resolver.type": "doh",
}, "."), nil)
if err := k.Load(file.Provider(cfgPath), toml.Parser()); err != nil {
return Config{}, err
}
cfg := Config{}
if err := k.Unmarshal("", &cfg); err != nil {
return Config{}, err
}
return cfg, nil
}