2020-02-07 10:41:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-12 16:39:57 +00:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-02-07 10:41:29 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-02-12 16:39:57 +00:00
|
|
|
"os"
|
2020-02-07 10:41:29 +00:00
|
|
|
|
2020-02-12 16:39:57 +00:00
|
|
|
core "github.com/ChronosX88/zirconium/core"
|
2020-02-10 10:55:22 +00:00
|
|
|
"github.com/google/logger"
|
2020-02-07 10:41:29 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/websocket"
|
2020-02-12 16:39:57 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
2020-02-07 10:41:29 +00:00
|
|
|
)
|
|
|
|
|
2020-02-12 16:39:57 +00:00
|
|
|
var connectionHandler = core.NewConnectionHandler()
|
2020-02-07 10:41:29 +00:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-02-12 16:39:57 +00:00
|
|
|
|
|
|
|
var cfg core.ServerConfig
|
|
|
|
var configPath string
|
|
|
|
var generateConfig bool
|
|
|
|
flag.StringVar(&configPath, "config", "", "Path to config")
|
|
|
|
flag.BoolVar(&generateConfig, "gen_config", false, "Generate the config")
|
|
|
|
flag.Parse()
|
|
|
|
if generateConfig == true {
|
|
|
|
sampleConfig := &core.ServerConfig{}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
core.InitializeContext(cfg.ServerDomain, cfg.PluginsDirPath, cfg.EnabledPlugins)
|
2020-02-07 10:41:29 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Write([]byte("Zirconium server is up and running!"))
|
|
|
|
}).Methods("GET")
|
|
|
|
router.HandleFunc("/ws", wsHandler)
|
|
|
|
|
2020-02-10 10:55:22 +00:00
|
|
|
logger.Info("Zirconium successfully started!")
|
|
|
|
logger.Fatal(http.ListenAndServe(":8844", router))
|
2020-02-07 10:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-02-10 09:23:10 +00:00
|
|
|
connectionHandler.HandleNewConnection(ws)
|
2020-02-07 10:41:29 +00:00
|
|
|
}
|
2020-02-12 16:39:57 +00:00
|
|
|
|
|
|
|
func validateConfig(config *core.ServerConfig) error {
|
|
|
|
if config.ServerDomain == "" {
|
|
|
|
return errors.New("server domain isn't specified")
|
|
|
|
} else if config.PluginsDirPath == "" {
|
|
|
|
return errors.New("plugin directory path isn't specified")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|