HandleJSON wraps a JSONHandler into an http.HandlerFunc to handle JSON requests.
(w http.ResponseWriter, r *http.Request, req Req, h JSONHandler[Req, Resp])
| 155 | |
| 156 | // HandleJSON wraps a JSONHandler into an http.HandlerFunc to handle JSON requests. |
| 157 | func HandleJSON[Req RequestObject, Resp any](w http.ResponseWriter, r *http.Request, |
| 158 | req Req, h JSONHandler[Req, Resp]) { |
| 159 | |
| 160 | if err := ReadRequest(r, req); err != nil { |
| 161 | ErrorHandler(r, w, err) |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | ctx, cancel := ctxcache.Init(r.Context()) |
| 166 | defer cancel() |
| 167 | |
| 168 | w.Header().Set("Content-Type", "application/json") |
| 169 | resp := h(ctx, req) |
| 170 | |
| 171 | if err := jsonflow.MarshalWrite(w, resp); err != nil { |
| 172 | ErrorHandler(r, w, err) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Event represents an SSE (Server-Sent Event) with optional |
| 177 | // ID, event type, data, and retry interval. |
nothing calls this directly
no test coverage detected