From 1675882b8423d8a9263916e5355d3943b33a6b1b Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 5 Mar 2023 20:52:45 +0100 Subject: [PATCH] improve http api request handling --- pkg/server/api.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/server/api.go b/pkg/server/api.go index f9324d7..a23b0c3 100644 --- a/pkg/server/api.go +++ b/pkg/server/api.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "runtime/debug" + "time" "golang.org/x/exp/slog" ) @@ -13,10 +14,16 @@ func HandleAPI(w http.ResponseWriter, r *http.Request) { defer func() { if r := recover(); r != nil { slog.Error("Recovered Panic Handling HTTP API Request", fmt.Errorf("%v", r), "stack", debug.Stack()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return } }() - err := apiHandler.HandleRequest(context.TODO(), r.Body, w) + ctx, cancel := context.WithTimeout(r.Context(), time.Second*10) + defer cancel() + + err := apiHandler.HandleRequest(ctx, r.Body, w) if err != nil { + w.WriteHeader(500) slog.Error("Handling HTTP API Request", err) } }