package main
import (
"flag"
"github.com/BurntSushi/toml"
"log"
"os"
"strconv"
)
var administrators map[string]bool
type conf struct {
System systemConf `toml:"system"`
Server serverConf `toml:"server"`
Discord discordConf `toml:"discord"`
}
type serverConf struct {
Host string `toml:"host"`
Unix bool `toml:"unix"`
Port uint16 `toml:"port"`
Proxy bool `toml:"proxy"`
TrustedProxies []string `toml:"trusted_proxies"`
BaseURL string `toml:"base_url"`
Secret string `toml:"secret"`
}
type systemConf struct {
Verbose bool `toml:"verbose"`
Store string `toml:"store"`
}
type discordConf struct {
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
Administrators []int `toml:"administrators"`
}
var (
config conf
confPath string
confRead = false
)
func init() {
flag.StringVar(&confPath, "c", "server.conf", "specify path to configuration file")
}
func confLoad() {
if confRead {
panic("configuration read called when already read")
}
defer func() { confRead = true }()
if meta, err := toml.DecodeFile(confPath, &config); err != nil {
if !os.IsNotExist(err) {
log.Fatalf("error parsing configuration: %s", err)
}
var file *os.File
if file, err = os.Create(confPath); err != nil {
log.Fatalf("error creating configuration file: %s", err)
} else if err = toml.NewEncoder(file).Encode(defConf); err != nil {
log.Fatalf("error generating default configuration: %s", err)
}
config = defConf
return
} else {
for _, key := range meta.Undecoded() {
log.Printf("unused key in configuration file: %s", key.String())
}
}
if len(config.Discord.Administrators) != 0 && config.Discord.Administrators[0] != -1 {
administrators = make(map[string]bool)
for _, id := range config.Discord.Administrators {
administrators[strconv.Itoa(id)] = true
}
}
}
var defConf = conf{
System: systemConf{
Verbose: false,
Store: "db",
},
Server: serverConf{
Host: "127.0.0.1",
Unix: false,
Port: 7777,
Proxy: true,
TrustedProxies: []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
},
BaseURL: "http://localhost:7777/",
Secret: "RANDOM_STRING",
},
Discord: discordConf{
ClientID: "",
ClientSecret: "",
Administrators: []int{-1},
},
}