Added "WithMaxBytes" middleware

This commit is contained in:
Robin Dittmar
2026-07-20 20:12:45 +02:00
parent 10a34387ef
commit caf79e0dff
3 changed files with 14 additions and 2 deletions
+13
View File
@@ -0,0 +1,13 @@
package middleware
import "net/http"
func WithMaxBytes(limit int64) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, limit)
next.ServeHTTP(w, r)
})
}
}
-2
View File
@@ -14,8 +14,6 @@ type CreateListPayload struct {
func DecodeCreateList(r *http.Request) (CreateListPayload, error) {
var payload CreateListPayload
r.Body = http.MaxBytesReader(nil, r.Body, 1024*64)
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
+1
View File
@@ -28,6 +28,7 @@ func NewMux(cfg Config) http.Handler {
mux.HandleFunc("POST /lists", listHandler.CreateList)
var httpHandler http.Handler = mux
httpHandler = middleware.WithMaxBytes(1024 * 64)(httpHandler)
httpHandler = middleware.WithTelemetry(httpHandler)
return httpHandler