improve http api request handling

This commit is contained in:
Samuel Lorch 2023-03-05 20:52:45 +01:00
parent bc8ba876e1
commit 1675882b84

View file

@ -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)
}
}