mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
improve http api request handling
This commit is contained in:
parent
bc8ba876e1
commit
1675882b84
1 changed files with 8 additions and 1 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
|
@ -13,10 +14,16 @@ func HandleAPI(w http.ResponseWriter, r *http.Request) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
slog.Error("Recovered Panic Handling HTTP API Request", fmt.Errorf("%v", r), "stack", debug.Stack())
|
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 {
|
if err != nil {
|
||||||
|
w.WriteHeader(500)
|
||||||
slog.Error("Handling HTTP API Request", err)
|
slog.Error("Handling HTTP API Request", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue