refactor(webui): decode JSON request bodies with generic decodeBody
This commit is contained in:
1 parent
af5e11a734
commit
84b581fde6
7 files changed
+35
-40
No files matched your search
@@ -2,7 +2,6 @@ package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
@@ -103,9 +102,8 @@ func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req bindingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[bindingRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
b, ok := s.checkedBinding(w, r, "", req)
|
||||
@@ -188,9 +186,8 @@ func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var req bindingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[bindingRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
b, ok := s.checkedBinding(w, r, id, req)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
@@ -92,9 +91,8 @@ func (s *Server) handleEventTypeCreate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "event types not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req eventTypeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[eventTypeRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
t := eventtype.FromExample(req.Source, req.Name, eventtype.Sample{
|
||||
@@ -122,9 +120,8 @@ func (s *Server) handleEventTypeUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "event types not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req eventTypeUpdate
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[eventTypeUpdate](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Source is not editable: the store validates against the stored one.
|
||||
|
||||
@@ -184,9 +184,8 @@ func (s *Server) handleExtensionInstall(w http.ResponseWriter, r *http.Request)
|
||||
if !s.extensionsReady(w) {
|
||||
return
|
||||
}
|
||||
var request installExtensionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
request, ok := decodeBody[installExtensionRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
p, err := s.Packages.InstallPackage(r.Context(),
|
||||
@@ -227,9 +226,8 @@ func (s *Server) handleExtensionEnabled(w http.ResponseWriter, r *http.Request)
|
||||
if !s.extensionsReady(w) {
|
||||
return
|
||||
}
|
||||
var request extensionEnabledRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
request, ok := decodeBody[extensionEnabledRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := r.PathValue("name")
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -36,9 +35,8 @@ func (s *Server) handleIdentitiesList(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
|
||||
var request identityRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
request, ok := decodeBody[identityRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := randomIdentityID()
|
||||
@@ -64,9 +62,8 @@ func (s *Server) handleIdentityCreate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) handleIdentityCommand(w http.ResponseWriter, r *http.Request) {
|
||||
var request identityRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
request, ok := decodeBody[identityRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
audit, err := webAudit(r.Context())
|
||||
|
||||
@@ -2,7 +2,6 @@ package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
@@ -72,9 +71,8 @@ func (s *Server) handleMappingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "mappings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req mappingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[mappingRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
m := mapping.Mapping{Name: req.Name, SourceHint: req.SourceHint, EventTypeID: req.EventTypeID, Fields: req.Fields}
|
||||
@@ -133,9 +131,8 @@ func (s *Server) handleMappingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "invalid mapping id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var req mappingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[mappingRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
m := mapping.Mapping{ID: id, Name: req.Name, SourceHint: req.SourceHint, EventTypeID: req.EventTypeID, Fields: req.Fields}
|
||||
@@ -202,9 +199,8 @@ func (s *Server) handleMappingPreview(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "captures not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req mappingPreviewRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
req, ok := decodeBody[mappingPreviewRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
capture, err := s.captureByID(r.Context(), req.CaptureID)
|
||||
|
||||
@@ -47,9 +47,8 @@ func (s *Server) handleWorkflowEnabled(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var request workflowEnabledRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
request, ok := decodeBody[workflowEnabledRequest](w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
|
||||
@@ -432,3 +432,14 @@ func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
// decodeBody decodes the JSON request body into a T, answering 400 itself
|
||||
// when the body is invalid.
|
||||
func decodeBody[T any](w http.ResponseWriter, r *http.Request) (T, bool) {
|
||||
var v T
|
||||
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return v, false
|
||||
}
|
||||
return v, true
|
||||
}
|
||||
Reference in new issue
Block a user