DustyP 9 months ago
parent 1cda34fe93
commit 9eae310b94

@ -12,8 +12,8 @@ import (
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"io/ioutil"
"log" "log"
"log/slog" "log/slog"
"math/big" "math/big"
@ -1815,27 +1815,33 @@ func (s *Server) handleRerunHeat() http.HandlerFunc {
} }
} }
func parseRequestBody(r *http.Request) (string, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// handleSetRacingGroup sets the currently racing group // handleSetRacingGroup sets the currently racing group
func (s *Server) handleSetRacingGroup() http.HandlerFunc { func (s *Server) handleSetRacingGroup() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body) text, err := parseRequestBody(r)
if err != nil { if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest) s.logger.Error("Failed to parse request body", "error", err)
http.Error(w, "Failed to parse request body", http.StatusBadRequest)
return return
} }
defer r.Body.Close()
// Process the plain text body
text := string(body)
s.logger.Info("Set racing group request", "text", text) s.logger.Info("Set racing group request", "text", text)
// Parse request body // Parse request body
var request struct { var request struct {
GroupID int64 `json:"group_id"` GroupID int64 `json:"group_id"`
} }
request.GroupID, err = strconv.ParseInt(strings.Split(text, "=")[1], 10, 64)
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) s.logger.Error("Failed to parse group ID", "error", err)
http.Error(w, "Failed to parse group ID", http.StatusBadRequest)
return return
} }

Loading…
Cancel
Save