30 lines
528 B
Go
30 lines
528 B
Go
package middleware
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func WithValidatedPost[K any](
|
|
fn func(request K, w http.ResponseWriter, r *http.Request),
|
|
) func(w http.ResponseWriter, r *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
request := new(K)
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(body, request)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
fn(*request, w, r)
|
|
}
|
|
}
|