http_ctl.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "time"
  9. fractal "git.lattuga.net/blallo/gotools/formatting"
  10. "git.lattuga.net/boyska/circolog"
  11. "github.com/gorilla/mux"
  12. )
  13. func setupHTTPCtl(hub circolog.Hub, verbose, debug bool) *mux.Router {
  14. m := mux.NewRouter()
  15. m.HandleFunc("/pause/toggle", togglePause(hub, verbose, debug)).Methods("POST")
  16. m.HandleFunc("/status", getStatus(hub, verbose, debug)).Methods("GET")
  17. m.HandleFunc("/logs/clear", clearQueue(hub, verbose)).Methods("POST")
  18. m.HandleFunc("/help", printHelp(verbose)).Methods("GET")
  19. m.HandleFunc("/echo", echo(verbose)).Methods("GET")
  20. return m
  21. }
  22. func getStatus(hub circolog.Hub, verbose, debug bool) http.HandlerFunc {
  23. return func(w http.ResponseWriter, r *http.Request) {
  24. response := make(chan circolog.CommandResponse)
  25. hub.Commands <- circolog.HubFullCommand{
  26. Command: circolog.CommandStatus,
  27. Response: response,
  28. }
  29. resp := <-response
  30. w.Header().Set("content-type", "application/json")
  31. enc := json.NewEncoder(w)
  32. enc.Encode(map[string]interface{}{"status": resp.Value})
  33. }
  34. }
  35. func togglePause(hub circolog.Hub, verbose, debug bool) http.HandlerFunc {
  36. return func(w http.ResponseWriter, r *http.Request) {
  37. if verbose {
  38. log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
  39. }
  40. r.ParseForm()
  41. waitTimePar := r.FormValue("waitTime")
  42. var waitTime time.Duration
  43. var err error
  44. if waitTimePar != "" {
  45. waitTime, err = time.ParseDuration(waitTimePar)
  46. if err != nil {
  47. w.WriteHeader(400)
  48. fmt.Fprintln(w, "waitTime not understood:", waitTimePar)
  49. return
  50. }
  51. }
  52. if debug {
  53. fmt.Println("[DEBUG] waitTime:", waitTime)
  54. }
  55. response := make(chan circolog.CommandResponse)
  56. hub.Commands <- circolog.HubFullCommand{
  57. Command: circolog.CommandPauseToggle,
  58. Response: response,
  59. Parameters: map[string]interface{}{"waitTime": waitTime},
  60. }
  61. resp := <-response
  62. active := resp.Value.(bool)
  63. w.Header().Set("content-type", "application/json")
  64. enc := json.NewEncoder(w)
  65. enc.Encode(map[string]interface{}{"paused": !active})
  66. }
  67. }
  68. func clearQueue(hub circolog.Hub, verbose bool) http.HandlerFunc {
  69. return func(w http.ResponseWriter, r *http.Request) {
  70. if verbose {
  71. log.Printf("[%s] %s - cleared queue", r.Method, r.RemoteAddr)
  72. }
  73. response := make(chan circolog.CommandResponse)
  74. hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear, Response: response}
  75. resp := <-response
  76. success := resp.Value.(bool)
  77. w.Header().Set("content-type", "application/json")
  78. enc := json.NewEncoder(w)
  79. enc.Encode(map[string]interface{}{"success": success})
  80. }
  81. }
  82. func printHelp(verbose bool) http.HandlerFunc {
  83. return func(w http.ResponseWriter, r *http.Request) {
  84. if verbose {
  85. log.Printf("[%s] %s - asked for help", r.Method, r.RemoteAddr)
  86. }
  87. w.Header().Set("content-type", "application/json")
  88. enc := json.NewEncoder(w)
  89. var pathsWithDocs = map[string]string{
  90. "/pause/toggle": "Toggle the server from pause state (not listening)",
  91. "/logs/clear": "Wipe the buffer from all the messages",
  92. "/status": "Get info on the status of the server",
  93. "/help": "This help",
  94. "/echo": "Answers to heartbeat",
  95. }
  96. fractal.EncodeJSON(pathsWithDocs, enc)
  97. if verbose {
  98. errEnc := json.NewEncoder(os.Stderr)
  99. fractal.EncodeJSON(pathsWithDocs, errEnc)
  100. }
  101. }
  102. }
  103. func echo(verbose bool) http.HandlerFunc {
  104. return func(w http.ResponseWriter, r *http.Request) {
  105. log.Println("Echo")
  106. if verbose {
  107. log.Printf("[%s] %s - asked for echo", r.Method, r.RemoteAddr)
  108. }
  109. w.Header().Set("content-type", "text/plain")
  110. fmt.Fprintln(w, "I am on!")
  111. }
  112. }