package main import ( "encoding/json" "net/http" ) type liveStreamAPIResponse struct { Enabled bool `json:"enabled"` Success bool `json:"success"` Error string `json:"error,omitempty"` } func mountLiveStreamAPI(mux *http.ServeMux, hub *wsHub) { mux.HandleFunc("/api/live-stream", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: serveLiveStreamGet(w, hub) case http.MethodPut: serveLiveStreamPut(w, r, hub) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } }) } func serveLiveStreamGet(w http.ResponseWriter, hub *wsHub) { enabled := false if hub != nil { enabled = hub.liveStreamEnabled() } writeJSON(w, http.StatusOK, liveStreamAPIResponse{Enabled: enabled, Success: true}) } func serveLiveStreamPut(w http.ResponseWriter, r *http.Request, hub *wsHub) { var body struct { Enable bool `json:"enable"` } if err := json.NewDecoder(r.Body).Decode(&body); err != nil { writeJSON(w, http.StatusBadRequest, liveStreamAPIResponse{Error: "invalid JSON"}) return } if hub != nil { hub.patchLiveStream(body.Enable) } writeJSON(w, http.StatusOK, liveStreamAPIResponse{ Enabled: body.Enable, Success: true, }) }