2020-02-07 10:41:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-12 16:39:57 +00:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-02-07 10:41:29 +00:00
|
|
|
|
2021-03-30 19:40:02 +00:00
|
|
|
"github.com/cadmium-im/zirconium-go/core"
|
2020-02-10 10:55:22 +00:00
|
|
|
"github.com/google/logger"
|
2020-02-12 16:39:57 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
2020-02-07 10:41:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-03-30 19:40:02 +00:00
|
|
|
var cfg core.Config
|
2020-02-12 16:39:57 +00:00
|
|
|
var configPath string
|
|
|
|
var generateConfig bool
|
2021-04-06 13:55:41 +00:00
|
|
|
|
|
|
|
defer logger.Init("auth-server", true, false, ioutil.Discard).Close() // TODO Make ability to use file for log output
|
2020-02-12 16:39:57 +00:00
|
|
|
flag.StringVar(&configPath, "config", "", "Path to config")
|
2021-04-06 13:55:41 +00:00
|
|
|
flag.BoolVar(&generateConfig, "gen-config", false, "Generate the config")
|
2020-02-12 16:39:57 +00:00
|
|
|
flag.Parse()
|
|
|
|
if generateConfig == true {
|
2021-03-30 19:40:02 +00:00
|
|
|
sampleConfig := &core.Config{}
|
2020-02-12 16:39:57 +00:00
|
|
|
val, err := toml.Marshal(sampleConfig)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Failed to generate config: %s", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println(string(val))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if configPath == "" {
|
|
|
|
logger.Error("Path to config isn't specified!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cfgData, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to read config!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = toml.Unmarshal(cfgData, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Failed to read config! (yaml error: %s)", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = validateConfig(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Config validation failed: %s", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:40:02 +00:00
|
|
|
ac := core.NewAppContext(&cfg)
|
2020-02-10 10:55:22 +00:00
|
|
|
logger.Info("Zirconium successfully started!")
|
2021-03-30 19:40:02 +00:00
|
|
|
logger.Fatal(ac.Run())
|
2020-02-07 10:41:29 +00:00
|
|
|
}
|
2020-02-12 16:39:57 +00:00
|
|
|
|
2021-03-30 19:40:02 +00:00
|
|
|
func validateConfig(config *core.Config) error {
|
|
|
|
if config.ServerID == "" {
|
|
|
|
return errors.New("server id isn't specified")
|
2020-02-12 16:39:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|