forked from alice-lg/birdwatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
70 lines (59 loc) · 1.44 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
package main
// Birdwatcher Configuration
import (
"fmt"
"log"
"strings"
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"
"github.com/alice-lg/birdwatcher/bird"
"github.com/alice-lg/birdwatcher/endpoints"
)
type Config struct {
Server endpoints.ServerConfig
Ratelimit bird.RateLimitConfig
Status bird.StatusConfig
Bird bird.BirdConfig
Bird6 bird.BirdConfig
Parser bird.ParserConfig
Cache bird.CacheConfig
Housekeeping HousekeepingConfig
}
// Try to load configfiles as specified in the files
// list. For example:
//
// ./etc/birdwatcher/birdwatcher.conf
// /etc/birdwatcher/birdwatcher.conf
// ./etc/birdwatcher/birdwatcher.local.conf
//
//
func LoadConfigs(configFiles []string) (*Config, error) {
config := &Config{}
hasConfig := false
var confError error
for _, filename := range configFiles {
tmp := &Config{}
_, err := toml.DecodeFile(filename, tmp)
if err != nil {
continue
} else {
log.Println("Using config file:", filename)
hasConfig = true
// Merge configs
if err := mergo.Merge(config, tmp); err != nil {
return nil, err
}
}
}
if !hasConfig {
confError = fmt.Errorf("Could not load any config file")
}
return config, confError
}
func ConfigOptions(filename string) []string {
return []string{
strings.Join([]string{"/", filename}, ""),
strings.Join([]string{"./", filename}, ""),
strings.Replace(filename, ".conf", ".local.conf", 1),
}
}