Skip to content

Commit fbd9743

Browse files
committed
feat: add panic recovery middleware to enhance server stability
1 parent c54c6f9 commit fbd9743

File tree

9 files changed

+35
-9
lines changed

9 files changed

+35
-9
lines changed

app_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ func newApp(config AppConfig) *app {
260260
routerOpts = append(routerOpts, internalrouter.WithHTTPAddress(config.HTTPAddress))
261261
}
262262

263+
// Enable panic recovery to prevent unhandled panics from crashing the server
264+
routerOpts = append(routerOpts, internalrouter.WithRecovery())
265+
263266
router := NewRouter(routerOpts...)
264267

265268
// Register core services with DI - Both key-based (legacy) and type-based (new pattern)

examples/webtransport/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ require (
6969
github.com/uptrace/bunrouter v1.0.23 // indirect
7070
github.com/x448/float16 v0.8.4 // indirect
7171
github.com/xraph/confy v0.5.0 // indirect
72-
github.com/xraph/forgeui v1.4.0 // indirect
72+
github.com/xraph/forgeui v1.4.1 // indirect
7373
github.com/xraph/go-utils v1.1.0 // indirect
7474
github.com/xraph/vessel v1.0.0 // indirect
7575
go.uber.org/multierr v1.11.0 // indirect

examples/webtransport/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
276276
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
277277
github.com/xraph/confy v0.5.0 h1:7dK3hx3MQKlNPK9mFSm07iyU05kUnx6um8d86/gyajg=
278278
github.com/xraph/confy v0.5.0/go.mod h1:/uhVfKibPR+kn7MI9LkVVekk84NP0sxsKZ9sFQoQ5Kc=
279-
github.com/xraph/forgeui v1.4.0 h1:RhpRbecimGCSDf0XQnJhl5PmQjbxrF5M2PGEk6F60sg=
280-
github.com/xraph/forgeui v1.4.0/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
279+
github.com/xraph/forgeui v1.4.1 h1:LHK1t/sZ+9zL+MNUZralO9/rc0f5UCa19dpbWTuRMNg=
280+
github.com/xraph/forgeui v1.4.1/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
281281
github.com/xraph/go-utils v1.1.0 h1:bPO4C/mwm0I5ZZa3RxbwUqKhy3Vnxvm3s98vmX8CrjY=
282282
github.com/xraph/go-utils v1.1.0/go.mod h1:tZN4SuGy9otCo6dETp7Cvkgyiy0BvaJaZbTBv4Euvo4=
283283
github.com/xraph/vessel v1.0.0 h1:n2q30d0OGPENpFfmOUgEuS99Y+X6b6WTfzdOHiE4Ds0=

extensions/dashboard/extension.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ func (e *Extension) initializeForgeUI() {
713713
e.Logger().Warn("forgeui initialization warning", forge.F("error", err.Error()))
714714
}
715715

716+
// Add panic recovery middleware so dashboard page panics don't crash the server
717+
fuiApp.Use(router.Recovery())
718+
716719
e.fuiApp = fuiApp
717720

718721
// Override ScriptURL so component Script() tags resolve to the dashboard's

extensions/streaming/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/redis/go-redis/v9 v9.14.1
1010
github.com/xraph/forge v1.2.0
1111
github.com/xraph/forge/extensions/auth v0.0.0-20260115044919-4bf291c53376
12-
github.com/xraph/forgeui v1.4.0
12+
github.com/xraph/forgeui v1.4.1
1313
github.com/xraph/vessel v1.0.0
1414
)
1515

extensions/streaming/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
276276
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
277277
github.com/xraph/confy v0.5.0 h1:7dK3hx3MQKlNPK9mFSm07iyU05kUnx6um8d86/gyajg=
278278
github.com/xraph/confy v0.5.0/go.mod h1:/uhVfKibPR+kn7MI9LkVVekk84NP0sxsKZ9sFQoQ5Kc=
279-
github.com/xraph/forgeui v1.4.0 h1:RhpRbecimGCSDf0XQnJhl5PmQjbxrF5M2PGEk6F60sg=
280-
github.com/xraph/forgeui v1.4.0/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
279+
github.com/xraph/forgeui v1.4.1 h1:LHK1t/sZ+9zL+MNUZralO9/rc0f5UCa19dpbWTuRMNg=
280+
github.com/xraph/forgeui v1.4.1/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
281281
github.com/xraph/go-utils v1.1.0 h1:bPO4C/mwm0I5ZZa3RxbwUqKhy3Vnxvm3s98vmX8CrjY=
282282
github.com/xraph/go-utils v1.1.0/go.mod h1:tZN4SuGy9otCo6dETp7Cvkgyiy0BvaJaZbTBv4Euvo4=
283283
github.com/xraph/vessel v1.0.0 h1:n2q30d0OGPENpFfmOUgEuS99Y+X6b6WTfzdOHiE4Ds0=

extensions/webrtc/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ require (
8585
github.com/wlynxg/anet v0.0.3 // indirect
8686
github.com/x448/float16 v0.8.4 // indirect
8787
github.com/xraph/confy v0.5.0 // indirect
88-
github.com/xraph/forgeui v1.4.0 // indirect
88+
github.com/xraph/forgeui v1.4.1 // indirect
8989
github.com/xraph/go-utils v1.1.0 // indirect
9090
github.com/xraph/vessel v1.0.0 // indirect
9191
go.uber.org/multierr v1.11.0 // indirect

extensions/webrtc/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ github.com/xraph/confy v0.5.0 h1:7dK3hx3MQKlNPK9mFSm07iyU05kUnx6um8d86/gyajg=
327327
github.com/xraph/confy v0.5.0/go.mod h1:/uhVfKibPR+kn7MI9LkVVekk84NP0sxsKZ9sFQoQ5Kc=
328328
github.com/xraph/forge v1.2.0 h1:+f5pAoWIa4vzFDD3j5b3qDk+X52/Hr1kyoV6CV8vx0g=
329329
github.com/xraph/forge v1.2.0/go.mod h1:n7mKRcLgNsGblsMDmpBrZNieMSe7dFmnGaA1O8sDZSw=
330-
github.com/xraph/forgeui v1.4.0 h1:RhpRbecimGCSDf0XQnJhl5PmQjbxrF5M2PGEk6F60sg=
331-
github.com/xraph/forgeui v1.4.0/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
330+
github.com/xraph/forgeui v1.4.1 h1:LHK1t/sZ+9zL+MNUZralO9/rc0f5UCa19dpbWTuRMNg=
331+
github.com/xraph/forgeui v1.4.1/go.mod h1:rH/+wb1tt2pXSHotWAvoP+Lt846xlIjuwPDSpS5K5mw=
332332
github.com/xraph/go-utils v1.1.0 h1:bPO4C/mwm0I5ZZa3RxbwUqKhy3Vnxvm3s98vmX8CrjY=
333333
github.com/xraph/go-utils v1.1.0/go.mod h1:tZN4SuGy9otCo6dETp7Cvkgyiy0BvaJaZbTBv4Euvo4=
334334
github.com/xraph/vessel v1.0.0 h1:n2q30d0OGPENpFfmOUgEuS99Y+X6b6WTfzdOHiE4Ds0=

internal/router/router_impl.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ func (r *router) register(method, path string, handler any, opts ...RouteOption)
598598
r.container,
599599
r.errorHandler,
600600
)
601+
602+
// Wrap with panic recovery if enabled
603+
if r.recovery {
604+
inner := finalHandler
605+
logger := r.logger
606+
finalHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
607+
defer func() {
608+
if rec := recover(); rec != nil {
609+
if logger != nil {
610+
logger.Error(fmt.Sprintf("panic recovered: %v", rec))
611+
}
612+
613+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
614+
}
615+
}()
616+
617+
inner.ServeHTTP(w, req)
618+
})
619+
}
620+
601621
r.adapter.Handle(method, fullPath, finalHandler)
602622
}
603623

0 commit comments

Comments
 (0)