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.1KB

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