1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00

load and merge configs from system folder

This commit is contained in:
Matthias Hannig 2016-11-30 12:55:22 +01:00
parent b5ae11c105
commit 75b4aa3bd1
5 changed files with 118 additions and 10 deletions

View file

@ -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))
}

67
config.go Normal file
View file

@ -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
}

17
config_test.go Normal file
View file

@ -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)
}

View file

@ -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'

View file

@ -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'