// NGnius 2020-01-30 package main // leadercraft-server import ( "fmt" "net/http" "os" "os/signal" ) const ( // Version the current version Version = "0.1" // Name the program name Name = "leadercraft-s" ) var ( server *http.Server handler http.Handler port string root string isClosing bool ) func init() { initArgs() serverMux := http.NewServeMux() serverMux.HandleFunc("/load", boardHandler) serverMux.HandleFunc("/board", boardHandler) serverMux.HandleFunc("/player", playerHandler) serverMux.HandleFunc("/record", newEntryHandler) serverMux.HandleFunc("/token", newKeyHandler) handler = serverMux } func main() { parseArgs() sqlInitErr := sqlInit() if sqlInitErr != nil { fmt.Printf("Failed to initialise SQL connection: %s\n", sqlInitErr) os.Exit(1) } // handle interrupt (terminate) signal signalChan := make(chan os.Signal) signal.Notify(signalChan, os.Interrupt) go func() { s := <-signalChan fmt.Println("Received terminate signal " + s.String()) isClosing = true sqlClose() server.Close() }() server = &http.Server{ Addr: ":" + port, Handler: handler, } fmt.Println("Starting on " + server.Addr) //fmt.Println(server.ListenAndServe()) err := server.ListenAndServe() if err != nil && !isClosing { fmt.Println(err) } }