multiserver/config.go

58 lines
1.0 KiB
Go
Raw Normal View History

2021-01-05 11:34:35 -08:00
package multiserver
import (
"io/ioutil"
"os"
2021-01-05 11:34:35 -08:00
"strings"
"gopkg.in/yaml.v2"
)
2021-01-10 02:02:51 -08:00
var config map[interface{}]interface{}
2021-01-05 11:34:35 -08:00
var defaultConfig []byte = []byte(`host: "0.0.0.0:33000"
player_limit: -1
servers:
lobby:
address: "127.0.0.1:30000"
default_server: lobby
force_default_server: true
`)
2021-01-05 11:34:35 -08:00
// LoadConfig loads the configuration file
func LoadConfig() error {
os.Mkdir("config", 0775)
_, err := os.Stat("config/multiserver.yml")
if os.IsNotExist(err) {
2021-01-10 12:51:26 -08:00
ioutil.WriteFile("config/multiserver.yml", defaultConfig, 0664)
}
2021-01-05 11:34:35 -08:00
data, err := ioutil.ReadFile("config/multiserver.yml")
if err != nil {
return err
}
2021-01-09 03:26:30 -08:00
2021-01-10 02:02:51 -08:00
config = make(map[interface{}]interface{})
2021-01-09 03:26:30 -08:00
2021-01-10 02:02:51 -08:00
err = yaml.Unmarshal(data, &config)
2021-01-05 11:34:35 -08:00
if err != nil {
return err
}
2021-01-09 03:26:30 -08:00
2021-01-05 11:34:35 -08:00
return nil
}
// GetKey returns a key in the configuration
2021-01-06 05:42:55 -08:00
func GetConfKey(key string) interface{} {
2021-01-05 11:34:35 -08:00
keys := strings.Split(key, ":")
2021-01-10 02:02:51 -08:00
c := config
2021-01-09 03:26:30 -08:00
for i := 0; i < len(keys)-1; i++ {
2021-01-05 11:34:35 -08:00
if c[keys[i]] == nil {
return nil
}
c = c[keys[i]].(map[interface{}]interface{})
}
2021-01-09 03:26:30 -08:00
return c[keys[len(keys)-1]]
2021-01-05 11:34:35 -08:00
}