|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // 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)
- }
- }
|