diff --git a/birdwatcher.go b/birdwatcher.go index df3197e..8540d5a 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -28,19 +28,43 @@ func makeRouter() *httprouter.Router { return r } +// Print service information like, listen address, +// access restrictions and configuration flags +func PrintServiceInfo(conf *Config, birdConf BirdConfig) { + // General Info + log.Println("Starting Birdwatcher") + log.Println(" Using:", birdConf.BirdCmd) + log.Println(" Listen:", birdConf.Listen) +} + func main() { - port := flag.String("port", - "29184", - "The port the birdwatcher should run on") - birdc := flag.String("birdc", - "birdc", - "The birdc command to use (for IPv6, use birdc6)") + bird6 := flag.Bool("6", false, "Use bird6 instead of bird") flag.Parse() - bird.BirdCmd = *birdc + // Load configurations + conf, err := LoadConfigs([]string{ + "./etc/birdwatcher/birdwatcher.conf", + "/etc/birdwatcher/birdwatcher.conf", + "./etc/birdwatcher/birdwatcher.local.conf", + }) + + if err != nil { + log.Fatal("Loading birdwatcher configuration failed:", err) + } + + // Get config according to flags + birdConf := conf.Bird + if *bird6 { + birdConf = conf.Bird6 + } + + PrintServiceInfo(conf, birdConf) + + // Configure client + bird.BirdCmd = birdConf.BirdCmd r := makeRouter() - realPort := strings.Join([]string{":", *port}, "") + realPort := strings.Join([]string{":", "23022"}, "") log.Fatal(http.ListenAndServe(realPort, r)) } diff --git a/config.go b/config.go new file mode 100644 index 0000000..fea2b6f --- /dev/null +++ b/config.go @@ -0,0 +1,67 @@ +package main + +// Birdwatcher Configuration + +import ( + "fmt" + "github.com/BurntSushi/toml" + "github.com/imdario/mergo" +) + +type Config struct { + Server ServerConfig + Status StatusConfig + Bird BirdConfig + Bird6 BirdConfig +} + +type ServerConfig struct { + AllowFrom []string `toml:"allow_from"` +} + +type StatusConfig struct { + ReconfigTimestampSource string `toml:"reconfig_timestamp_source"` + ReconfigTimestampMatch string `toml:"reconfig_timestamp_match"` + + FilteredFields []string `toml:"filtered_fields"` +} + +type BirdConfig struct { + Listen string + ConfigFilename string `toml:"config"` + BirdCmd string `toml:"birdc"` +} + +// 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 +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..4770030 --- /dev/null +++ b/config_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "testing" +) + +func TestLoadConfigs(t *testing.T) { + t.Log("Loading configs") + res, err := LoadConfigs([]string{ + "./etc/birdwatcher/birdwatcher.conf", + "/etc/birdwatcher/birdwatcher.conf", + "./etc/birdwatcher/birdwatcher.local.conf", + }) + + t.Log(res) + t.Log(err) +} diff --git a/etc/init/bird4watcher.conf b/etc/init/bird4watcher.conf index 0293fff..a8834a6 100644 --- a/etc/init/bird4watcher.conf +++ b/etc/init/bird4watcher.conf @@ -10,5 +10,5 @@ respawn limit 20 10 start on starting birdwatcher stop on stopping birdwatcher -exec /opt/ecix/birdwatcher/bin/birdwatcher-linux-amd64 -birdc bird -port 29184 2>&1 | logger -i -t 'BIRD4 WATCHER' +exec /opt/ecix/birdwatcher/bin/birdwatcher-linux-amd64 2>&1 | logger -i -t 'BIRD4 WATCHER' diff --git a/etc/init/bird6watcher.conf b/etc/init/bird6watcher.conf index 642c095..7cada64 100644 --- a/etc/init/bird6watcher.conf +++ b/etc/init/bird6watcher.conf @@ -10,5 +10,5 @@ respawn limit 20 10 start on starting birdwatcher stop on stopping birdwatcher -exec /opt/ecix/birdwatcher/bin/birdwatcher-linux-amd64 -birdc bird6 -port 29185 2>&1 | logger -i -t 'BIRD6 WATCHER' +exec /opt/ecix/birdwatcher/bin/birdwatcher-linux-amd64 -6 2>&1 | logger -i -t 'BIRD6 WATCHER'