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.

65 lines
1.2KB

  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. handler = serverMux
  30. }
  31. func main() {
  32. parseArgs()
  33. sqlInitErr := sqlInit()
  34. if sqlInitErr != nil {
  35. fmt.Printf("Failed to initialise SQL connection: %s\n", sqlInitErr)
  36. os.Exit(1)
  37. }
  38. // handle interrupt (terminate) signal
  39. signalChan := make(chan os.Signal)
  40. signal.Notify(signalChan, os.Interrupt)
  41. go func() {
  42. s := <-signalChan
  43. fmt.Println("Received terminate signal " + s.String())
  44. isClosing = true
  45. sqlClose()
  46. server.Close()
  47. }()
  48. server = &http.Server{
  49. Addr: ":" + port,
  50. Handler: handler,
  51. }
  52. fmt.Println("Starting on " + server.Addr)
  53. //fmt.Println(server.ListenAndServe())
  54. err := server.ListenAndServe()
  55. if err != nil && !isClosing {
  56. fmt.Println(err)
  57. }
  58. }