|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ConfigPath string = "config.json"
|
|
|
|
|
|
|
|
type S_Config struct {
|
|
|
|
Address string `json:"Address"`
|
|
|
|
AdminSecret string `json:"AdminSecret"`
|
|
|
|
|
|
|
|
MaxLobbies int `json:"MaxLobbies"`
|
|
|
|
MaxPlayers int `json:"MaxPlayers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var Config S_Config
|
|
|
|
|
|
|
|
func initialize() error {
|
|
|
|
configExists, err := os.Open(ConfigPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("management.initialize: new deployment: building config")
|
|
|
|
|
|
|
|
configNew, err := os.OpenFile("config.json", os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err == nil {
|
|
|
|
Config.Address = ":8080"
|
|
|
|
Config.AdminSecret = "netadmin"
|
|
|
|
|
|
|
|
configByte, err := json.MarshalIndent(Config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("config.initialize: could not Marshall Settings: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configNew.Write(configByte)
|
|
|
|
} else {
|
|
|
|
log.Fatal("config.initialize: could not create new config.json file: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer configNew.Close()
|
|
|
|
} else {
|
|
|
|
configByte, err := ioutil.ReadAll(configExists)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("config.initialize: could not read config: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
json.Unmarshal(configByte, &Config)
|
|
|
|
|
|
|
|
defer configExists.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig() {
|
|
|
|
// If doesnt exist, write default config
|
|
|
|
|
|
|
|
initialize()
|
|
|
|
}
|