yanic/cmd/config.go

55 lines
1.0 KiB
Go
Raw Permalink Normal View History

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