chore: port lab 1 solution

This commit is contained in:
kk10
2026-10-02 11:45:26 +03:00
parent 65003b6119
commit 2f279a1715
43 changed files with 2402 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
package httpx
import (
"encoding/json"
"mime"
"net/http"
"mtusi2026-messenger/internal/dto"
)
func DecodeJSONBody(w http.ResponseWriter, r *http.Request, requestID string, dst any) bool {
if ct := r.Header.Get("Content-Type"); ct != "" {
mt, _, err := mime.ParseMediaType(ct)
if err != nil || mt != "application/json" {
WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "expected application/json", requestID)
return false
}
}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(dst); err != nil {
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
return false
}
return true
}