http_ctl.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.lattuga.net/boyska/circolog"
  6. "github.com/gorilla/mux"
  7. )
  8. func setupHTTPCtl(hub circolog.Hub) *mux.Router {
  9. mux := mux.NewRouter()
  10. mux.HandleFunc("/pause/toggle", togglePause(hub)).Methods("POST")
  11. mux.HandleFunc("/logs/clear", clearQueue(hub)).Methods("POST")
  12. return mux
  13. }
  14. func togglePause(hub circolog.Hub) http.HandlerFunc {
  15. return func(w http.ResponseWriter, r *http.Request) {
  16. hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
  17. resp := <-hub.Responses
  18. active := resp.Value.(bool)
  19. w.Header().Set("content-type", "application/json")
  20. enc := json.NewEncoder(w)
  21. enc.Encode(map[string]interface{}{"paused": !active})
  22. }
  23. }
  24. func clearQueue(hub circolog.Hub) http.HandlerFunc {
  25. return func(w http.ResponseWriter, r *http.Request) {
  26. hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear}
  27. resp := <-hub.Responses
  28. success := resp.Value.(bool)
  29. w.Header().Set("content-type", "application/json")
  30. enc := json.NewEncoder(w)
  31. enc.Encode(map[string]interface{}{"success": success})
  32. }
  33. }