package main import ( "encoding/json" "net/http" ) func mountLedRingAPI(mux *http.ServeMux, link *managedSerial) { mux.HandleFunc("/api/led-ring", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } serveLedRingPost(w, r, link) }) } func serveLedRingPost(w http.ResponseWriter, r *http.Request, link *managedSerial) { var body ledRingAPIRequest if err := json.NewDecoder(r.Body).Decode(&body); err != nil { writeJSON(w, http.StatusBadRequest, ledRingAPIResponse{Error: "invalid JSON"}) return } if body.Mode == "" { writeJSON(w, http.StatusBadRequest, ledRingAPIResponse{Error: "mode required"}) return } out := applyLedRing(link, body) status := http.StatusOK if out.Error != "" { status = http.StatusServiceUnavailable } else if !out.Success { status = http.StatusServiceUnavailable } writeJSON(w, status, out) }