-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
100 lines (84 loc) · 2.01 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
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
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"strings"
"time"
// "github.com/gdexlab/go-render/render"
)
type InvalidAddress int64
const (
IgnoreInvalidAddress InvalidAddress = 0
ProcessInvalidAddress = 1
DiscardInvalidAddress = 2
)
type Config struct {
Listen string `yaml:"listen"`
Prefix string `yaml:"prefix"`
Forwarders map[string]string `yaml:"forwarders"`
Default string `yaml:"default"`
IA InvalidAddress `yaml:"invalid-address"`
Static map[string]string `yaml:"static"`
Cache struct {
ExpTime time.Duration `yaml:"expiration"`
PurgeTime time.Duration `yaml:"purge"`
} `yaml:"cache"`
LogLevel string `yaml:"log-level"`
StrictIPv6 bool `yaml:"strict-ipv6"`
}
func (a InvalidAddress) String() string {
switch a {
case IgnoreInvalidAddress:
return "Ignore"
case ProcessInvalidAddress:
return "Process"
case DiscardInvalidAddress:
return "Discard"
}
return "Ignore"
}
func (ia *InvalidAddress) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
var IA string
err = unmarshal(&IA)
if err != nil {
return
}
switch strings.ToLower(IA) {
case "ignore":
*ia = IgnoreInvalidAddress
case "process":
*ia = ProcessInvalidAddress
case "discard":
*ia = DiscardInvalidAddress
default:
return fmt.Errorf("invalid-address must be one of 'ignore/process/discard'")
}
return nil
}
func InitConfig() (Config, error) {
fileName := flag.String("file", "config.yml", "config filename")
flag.Parse()
Configs, err := parseFile(*fileName)
if err != nil {
return Config{}, err
}
return *Configs, nil
}
func parseFile(filePath string) (*Config, error) {
cfg := new(Config)
body, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
cfg.Cache.ExpTime = 0
cfg.Cache.PurgeTime = 0
cfg.LogLevel = "info"
if err := yaml.UnmarshalStrict(body, &cfg); err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) validateForwarders() {
}