Follow the leader with help from a server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // NGnius 2020-01-30
  2. package main // leadercraft-server
  3. import (
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. )
  9. const (
  10. // Version the current version
  11. Version = "0.1"
  12. // Name the program name
  13. Name = "leadercraft-s"
  14. )
  15. var (
  16. server *http.Server
  17. handler http.Handler
  18. port string
  19. root string
  20. isClosing bool
  21. )
  22. func init() {
  23. initArgs()
  24. serverMux := http.NewServeMux()
  25. serverMux.HandleFunc("/load", boardHandler)
  26. serverMux.HandleFunc("/board", boardHandler)
  27. serverMux.HandleFunc("/player", playerHandler)
  28. serverMux.HandleFunc("/record", newEntryHandler)
  29. serverMux.HandleFunc("/token", newKeyHandler)
  30. handler = serverMux
  31. }
  32. func main() {
  33. parseArgs()
  34. sqlInitErr := sqlInit()
  35. if sqlInitErr != nil {
  36. fmt.Printf("Failed to initialise SQL connection: %s\n", sqlInitErr)
  37. os.Exit(1)
  38. }
  39. // handle interrupt (terminate) signal
  40. signalChan := make(chan os.Signal)
  41. signal.Notify(signalChan, os.Interrupt)
  42. go func() {
  43. s := <-signalChan
  44. fmt.Println("Received terminate signal " + s.String())
  45. isClosing = true
  46. sqlClose()
  47. server.Close()
  48. }()
  49. server = &http.Server{
  50. Addr: ":" + port,
  51. Handler: handler,
  52. }
  53. fmt.Println("Starting on " + server.Addr)
  54. //fmt.Println(server.ListenAndServe())
  55. err := server.ListenAndServe()
  56. if err != nil && !isClosing {
  57. fmt.Println(err)
  58. }
  59. }