(w http.ResponseWriter, r *http.Request, fn func(context.Context, Req) (*Resp, error))
| 395 | } |
| 396 | |
| 397 | func handleRequest[Req, Resp any](w http.ResponseWriter, r *http.Request, fn func(context.Context, Req) (*Resp, error)) { |
| 398 | var req Req |
| 399 | if r.Method != "GET" { |
| 400 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 401 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 402 | return |
| 403 | } |
| 404 | } |
| 405 | resp, err := fn(r.Context(), req) |
| 406 | if err != nil { |
| 407 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 408 | return |
| 409 | } |
| 410 | respData, err := json.Marshal(resp) |
| 411 | if err != nil { |
| 412 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 413 | return |
| 414 | } |
| 415 | w.Header().Set("Content-Type", "application/json") |
| 416 | w.WriteHeader(http.StatusOK) |
| 417 | if _, err := w.Write(respData); err != nil { |
| 418 | logctx.Warn(r.Context(), "writing http response", zap.Error(err)) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | func getSaltFromRequest(r *http.Request) (*blobcache.CID, error) { |
| 423 | saltStr := r.Header.Get("X-Salt") |
no test coverage detected