goufy/pkg/config/config.go

54 lines
860 B
Go

package config
import (
"io/ioutil"
"log"
yaml "gopkg.in/yaml.v3"
)
const CONFIGFILE = "config.yaml"
const DEFAULTCONFIG = `
trackmania:
host: "127.0.0.1"
port: 5000
api:
port: 5111
`
type TrackmaniaConfig struct {
Host string
Port int
}
type ApiConfig struct {
Port int
}
type AppConfig struct {
Trackmania TrackmaniaConfig
Api ApiConfig
}
func (c *AppConfig) ReadConfig() {
// read default config
err := yaml.Unmarshal([]byte(DEFAULTCONFIG), c)
if err != nil {
log.Fatal(err)
}
log.Print("Initiated the default config")
// open config.yaml
yamlFile, err := ioutil.ReadFile(CONFIGFILE)
if err != nil {
log.Fatal(err)
}
log.Print("Done reading yaml File")
// configure config from config.yaml
err = yaml.Unmarshal([]byte(yamlFile), c)
if err != nil {
log.Fatal(err)
}
log.Print("Read config from yaml File")
}