changes to loading config defaults

This commit is contained in:
fanyx 2021-04-09 00:26:11 +02:00
parent 326dbee9fd
commit c9ac76e40b
2 changed files with 12 additions and 15 deletions

View File

@ -7,7 +7,7 @@ import (
func main() { func main() {
// initiate config // initiate config
var conf config.AppConfig var conf = config.LoadDefaults()
conf.ReadConfig() conf.ReadConfig()
//fmt.Printf("%#v\n", conf) //fmt.Printf("%#v\n", conf)

View File

@ -7,15 +7,9 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
const CONFIGFILE = "config.yaml" var (
CONFIGFILE = "config.yaml"
const DEFAULTCONFIG = ` )
trackmania:
host: "127.0.0.1"
port: 5000
api:
port: 5111
`
type TrackmaniaConfig struct { type TrackmaniaConfig struct {
Host string Host string
@ -32,11 +26,6 @@ type AppConfig struct {
} }
func (c *AppConfig) ReadConfig() { 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") log.Print("Initiated the default config")
// open config.yaml // open config.yaml
yamlFile, err := ioutil.ReadFile(CONFIGFILE) yamlFile, err := ioutil.ReadFile(CONFIGFILE)
@ -51,3 +40,11 @@ func (c *AppConfig) ReadConfig() {
} }
log.Print("Read config from yaml File") log.Print("Read config from yaml File")
} }
func LoadDefaults() AppConfig {
var c AppConfig
c.Trackmania.Host = "127.0.0.1"
c.Trackmania.Port = 5000
c.Api.Port = 5111
return c
}