fix deadlocks and add recovery middleware for http server errors
All checks were successful
pedestrian-simulator / build (push) Successful in 1m1s

This commit is contained in:
2026-01-11 20:34:06 -07:00
parent 512b36b10e
commit 2286422503
2 changed files with 59 additions and 14 deletions

View File

@@ -11,6 +11,19 @@ import (
_ "github.com/go-sql-driver/mysql"
)
// RecoveryMiddleware catches panics and returns 500
func RecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("[PANIC] %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}
// getStepManager creates a new StepManager for the given user, loading state from DB
func getStepManager(userID string) *StepManager {
return NewStepManager(userID)
@@ -125,5 +138,5 @@ func main() {
// 6. Start Server
binding := "0.0.0.0:8080"
fmt.Printf("Server starting on http://%s\n", binding)
log.Fatal(http.ListenAndServe(binding, nil))
log.Fatal(http.ListenAndServe(binding, RecoveryMiddleware(http.DefaultServeMux)))
}