修复 radius 验证测试 panic 的问题

This commit is contained in:
bjdgyc
2025-03-25 17:36:25 +08:00
parent abce53e008
commit 946e138a48
4 changed files with 24 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package admin
import (
"fmt"
"net/http"
"runtime/debug"
"time"
"github.com/bjdgyc/anylink/base"
@@ -120,3 +121,17 @@ func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}
func recoverHttp(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
base.Error(err, string(stack))
// http.Error(w, "Internal Server Error", 500)
RespError(w, 500, "Internal Server Error")
}
}()
next.ServeHTTP(w, r)
})
}

View File

@@ -21,6 +21,7 @@ var UiData embed.FS
func StartAdmin() {
r := mux.NewRouter()
r.Use(recoverHttp, authMiddleware, handlers.CompressHandler)
// 所有路由添加安全头
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
@@ -29,8 +30,6 @@ func StartAdmin() {
next.ServeHTTP(w, req)
})
})
r.Use(authMiddleware)
r.Use(handlers.CompressHandler)
// 监控检测
r.HandleFunc("/status.html", func(w http.ResponseWriter, r *http.Request) {