goufy/pkg/config/config.go

56 lines
843 B
Go
Raw Normal View History

2021-04-02 18:53:25 +02:00
package config
import (
"io/ioutil"
2021-04-06 17:11:10 +02:00
"log"
2021-04-02 18:53:25 +02:00
yaml "gopkg.in/yaml.v3"
)
2021-04-09 00:26:11 +02:00
var (
CONFIGFILE = "config.yaml"
)
2021-04-06 17:11:10 +02:00
type TrackmaniaConfig struct {
Host string
Port int
}
2021-04-07 16:14:15 +02:00
type ApiConfig struct {
Port int
}
2021-04-06 17:11:10 +02:00
type AppConfig struct {
Trackmania TrackmaniaConfig
2021-04-07 16:14:15 +02:00
Api ApiConfig
2021-04-02 18:53:25 +02:00
}
2021-04-06 17:11:10 +02:00
func (c *AppConfig) ReadConfig() {
2021-04-07 16:14:15 +02:00
log.Print("Initiated the default config")
// open config.yaml
2021-04-02 18:53:25 +02:00
yamlFile, err := ioutil.ReadFile(CONFIGFILE)
if err != nil {
2021-04-06 17:11:10 +02:00
log.Fatal(err)
2021-04-02 18:53:25 +02:00
}
2021-04-07 16:14:15 +02:00
log.Print("Done reading yaml File")
// configure config from config.yaml
2021-04-02 18:53:25 +02:00
err = yaml.Unmarshal([]byte(yamlFile), c)
if err != nil {
2021-04-06 17:11:10 +02:00
log.Fatal(err)
2021-04-02 18:53:25 +02:00
}
2021-04-07 16:14:15 +02:00
log.Print("Read config from yaml File")
2021-04-02 18:53:25 +02:00
}
2021-04-09 00:26:11 +02:00
func LoadDefaults() AppConfig {
2021-04-09 01:32:13 +02:00
var c = AppConfig{
Trackmania: TrackmaniaConfig{
Host: "127.0.0.1",
Port: 5000,
},
Api: ApiConfig{
Port: 5111,
},
}
2021-04-09 00:26:11 +02:00
return c
}