MCPcopy Index your code
hub / github.com/go-spring/go-spring / HandleJSON

Function HandleJSON

stdlib/httpsvr/handler.go:157–174  ·  view source on GitHub ↗

HandleJSON wraps a JSONHandler into an http.HandlerFunc to handle JSON requests.

(w http.ResponseWriter, r *http.Request,
	req Req, h JSONHandler[Req, Resp])

Source from the content-addressed store, hash-verified

155
156// HandleJSON wraps a JSONHandler into an http.HandlerFunc to handle JSON requests.
157func 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.

Callers

nothing calls this directly

Calls 5

ContextMethod · 0.80
ReadRequestFunction · 0.70
InitMethod · 0.65
hFunction · 0.50
SetMethod · 0.45

Tested by

no test coverage detected