goufy/pkg/config/config.go

54 lines
860 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"
)
const CONFIGFILE = "config.yaml"
2021-04-06 17:11:10 +02:00
const DEFAULTCONFIG = `
2021-04-07 16:14:15 +02:00
trackmania:
host: "127.0.0.1"
port: 5000
api:
port: 5111
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
// read default config
err := yaml.Unmarshal([]byte(DEFAULTCONFIG), c)
if err != nil {
log.Fatal(err)
}
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
}