2016-11-30 12:55:22 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Birdwatcher Configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-30 15:19:01 +01:00
|
|
|
|
2016-11-30 12:55:22 +01:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/imdario/mergo"
|
2016-11-30 13:40:34 +01:00
|
|
|
|
|
|
|
"github.com/ecix/birdwatcher/bird"
|
2016-11-30 15:19:01 +01:00
|
|
|
"github.com/ecix/birdwatcher/endpoints"
|
2016-11-30 12:55:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2016-11-30 15:19:01 +01:00
|
|
|
Server endpoints.ServerConfig
|
2016-11-30 13:40:34 +01:00
|
|
|
|
2016-12-13 10:49:18 +01:00
|
|
|
Ratelimit bird.RateLimitConfig
|
|
|
|
Status bird.StatusConfig
|
|
|
|
Bird bird.BirdConfig
|
|
|
|
Bird6 bird.BirdConfig
|
2017-02-22 18:09:45 +01:00
|
|
|
Parser bird.ParserConfig
|
2016-11-30 12:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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
|
|
|
|
}
|