http_ctl.go 660 B

1234567891011121314151617181920212223242526
  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. return mux
  12. }
  13. func togglePause(hub circolog.Hub) http.HandlerFunc {
  14. return func(w http.ResponseWriter, r *http.Request) {
  15. hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
  16. resp := <-hub.Responses
  17. active := resp.Value.(bool)
  18. w.Header().Set("content-type", "application/json")
  19. enc := json.NewEncoder(w)
  20. enc.Encode(map[string]interface{}{"paused": !active})
  21. }
  22. }