-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
57 lines (46 loc) · 1.19 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
package traffikey
import (
"encoding/json"
"fmt"
"os"
log "github.com/sirupsen/logrus"
)
type Config struct {
Targets []*Target `json:"targets"`
Etcd *etcdConfig `json:"etcd"`
Traefik *traefikConfig `json:"traefik"`
}
type etcdConfig struct {
Endpoints []string `json:"endpoints"`
SSL bool `json:"ssl"`
}
type traefikConfig struct {
DefaultPrefix string `json:"default_prefix"`
DefaultEntrypoint string `json:"default_entrypoint"`
}
func NewConfig(filename string) (*Config, error) {
log.WithField("filename", filename).Debugf("reading configuration file")
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("configuration file %s doesn't exists", filename)
}
return nil, fmt.Errorf("failed to read configuration file %s: %v", filename, err)
}
cfg := new(Config)
err = json.NewDecoder(f).Decode(cfg)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON: %v", err)
}
// Make are all parts of the config aren't nil
if cfg.Etcd == nil {
cfg.Etcd = new(etcdConfig)
}
if cfg.Targets == nil {
cfg.Targets = []*Target{}
}
if cfg.Traefik == nil {
cfg.Traefik = new(traefikConfig)
}
return cfg, nil
}