refactor(handlers): extract request validation logic to new function
Extract the request validation logic from ValidateRequest into a new ValidateRequestWithBody function to allow passing pre-read request bodies. Update all handler functions to use the new validation function for consistency and to avoid duplicate body reading.
This commit is contained in:
@@ -108,7 +108,7 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequest(w, r)
|
||||
userID, _, err := ValidateRequestWithBody(w, r, body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
@@ -185,7 +185,14 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequest(w, r)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "failed to read request body")
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
userID, _, err := ValidateRequestWithBody(w, r, body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
@@ -263,7 +270,7 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequest(w, r)
|
||||
userID, _, err := ValidateRequestWithBody(w, r, body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
@@ -393,7 +400,14 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequest(w, r)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "failed to read request body")
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
userID, _, err := ValidateRequestWithBody(w, r, body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user