yanic/cmd/config.go

54 lines
1011 B
Go
Raw Permalink Normal View History

2017-09-17 01:26:19 +00:00
package cmd
import (
"fmt"
2018-01-13 13:41:49 +00:00
"io/ioutil"
2017-09-17 01:26:19 +00:00
"os"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/runtime"
2018-01-13 13:41:49 +00:00
"github.com/FreifunkBremen/yanic/webserver"
"github.com/naoina/toml"
2017-09-17 01:26:19 +00:00
)
2018-01-13 13:41:49 +00:00
// Config represents the whole configuration
type Config struct {
Respondd respond.Config
Webserver webserver.Config
Nodes runtime.NodesConfig
Database database.Config
}
2017-09-17 01:26:19 +00:00
var (
2018-01-19 03:31:33 +00:00
configPath string
collector *respond.Collector
nodes *runtime.Nodes
2017-09-17 01:26:19 +00:00
)
2018-01-13 13:41:49 +00:00
func loadConfig() *Config {
config, err := ReadConfigFile(configPath)
2017-09-17 01:26:19 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, "unable to load config file:", err)
os.Exit(2)
}
return config
}
2018-01-13 13:41:49 +00:00
// ReadConfigFile reads a config model from path of a yml file
func ReadConfigFile(path string) (config *Config, err error) {
config = &Config{}
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = toml.Unmarshal(file, config)
if err != nil {
return nil, err
}
return
}