goufy/pkg/config/config.go

51 lines
803 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 {
var c AppConfig
c.Trackmania.Host = "127.0.0.1"
c.Trackmania.Port = 5000
c.Api.Port = 5111
return c
}