http_ctl.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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("/logs/clear", clearQueue(hub, verbose)).Methods("POST")
  17. m.HandleFunc("/help", printHelp(verbose)).Methods("GET")
  18. m.HandleFunc("/echo", echo(verbose)).Methods("GET")
  19. return m
  20. }
  21. func togglePause(hub circolog.Hub, verbose, debug bool) http.HandlerFunc {
  22. return func(w http.ResponseWriter, r *http.Request) {
  23. if verbose {
  24. log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
  25. }
  26. r.ParseForm()
  27. waitTimePar := r.FormValue("waitTime")
  28. var waitTime time.Duration
  29. var err error
  30. if waitTimePar != "" {
  31. waitTime, err = time.ParseDuration(waitTimePar)
  32. if err != nil {
  33. w.WriteHeader(400)
  34. fmt.Fprintln(w, "waitTime not understood:", waitTimePar)
  35. return
  36. }
  37. }
  38. if debug {
  39. fmt.Println("[DEBUG] waitTime:", waitTime)
  40. }
  41. response := make(chan circolog.CommandResponse)
  42. hub.Commands <- circolog.HubFullCommand{
  43. Command: circolog.CommandPauseToggle,
  44. Response: response,
  45. Parameters: map[string]interface{}{"waitTime": waitTime},
  46. }
  47. resp := <-response
  48. active := resp.Value.(bool)
  49. w.Header().Set("content-type", "application/json")
  50. enc := json.NewEncoder(w)
  51. enc.Encode(map[string]interface{}{"paused": !active})
  52. }
  53. }
  54. func clearQueue(hub circolog.Hub, verbose bool) http.HandlerFunc {
  55. return func(w http.ResponseWriter, r *http.Request) {
  56. if verbose {
  57. log.Printf("[%s] %s - cleared queue", r.Method, r.RemoteAddr)
  58. }
  59. response := make(chan circolog.CommandResponse)
  60. hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear, Response: response}
  61. resp := <-response
  62. success := resp.Value.(bool)
  63. w.Header().Set("content-type", "application/json")
  64. enc := json.NewEncoder(w)
  65. enc.Encode(map[string]interface{}{"success": success})
  66. }
  67. }
  68. func printHelp(verbose bool) http.HandlerFunc {
  69. return func(w http.ResponseWriter, r *http.Request) {
  70. if verbose {
  71. log.Printf("[%s] %s - asked for help", r.Method, r.RemoteAddr)
  72. }
  73. w.Header().Set("content-type", "application/json")
  74. enc := json.NewEncoder(w)
  75. var pathsWithDocs = map[string]string{
  76. "/pause/toggle": "Toggle the server from pause state (not listening)",
  77. "/logs/clear": "Wipe the buffer from all the messages",
  78. "/help": "This help",
  79. "/echo": "Answers to heartbeat",
  80. }
  81. fractal.EncodeJSON(pathsWithDocs, enc)
  82. if verbose {
  83. errEnc := json.NewEncoder(os.Stderr)
  84. fractal.EncodeJSON(pathsWithDocs, errEnc)
  85. }
  86. }
  87. }
  88. func echo(verbose bool) http.HandlerFunc {
  89. return func(w http.ResponseWriter, r *http.Request) {
  90. log.Println("Echo")
  91. if verbose {
  92. log.Printf("[%s] %s - asked for echo", r.Method, r.RemoteAddr)
  93. }
  94. w.Header().Set("content-type", "text/plain")
  95. fmt.Fprintln(w, "I am on!")
  96. }
  97. }