mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
29 lines
632 B
Go
29 lines
632 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"runtime/debug"
|
|
"time"
|
|
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
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
|
|
}
|
|
}()
|
|
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)
|
|
}
|
|
}
|