u
This commit is contained in:
1 parent
4ce951dd56
commit
8a3dea8545
44 files changed
+1599
-30
No files matched your search
@@ -1,28 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"allapp/api/middleware"
|
||||
"allapp/api/router"
|
||||
"allapp/api/router/middleware"
|
||||
"allapp/config"
|
||||
"allapp/utils/responsex"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
recoverer "github.com/gofiber/fiber/v3/middleware/recover"
|
||||
recover2 "github.com/gofiber/fiber/v3/middleware/recover"
|
||||
"github.com/gofiber/fiber/v3/middleware/requestid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
//func errorHandler(ctx fiber.Ctx, err error) error {
|
||||
//
|
||||
//}
|
||||
type structValidator struct {
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
// Validate Validator needs to implement the Validate method
|
||||
func (v *structValidator) Validate(out any) error {
|
||||
return v.validate.Struct(out)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := config.InitConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(recoverer.New(), requestid.New(), middleware.Logger())
|
||||
app := fiber.New(fiber.Config{
|
||||
StructValidator: &structValidator{validate: validator.New()},
|
||||
ErrorHandler: func(ctx fiber.Ctx, err error) error {
|
||||
log.WithContext(ctx.Context()).Errorw("系统异常",
|
||||
"method", ctx.Method(),
|
||||
"path", ctx.Path(),
|
||||
"status", ctx.Response().StatusCode(),
|
||||
"error", err,
|
||||
)
|
||||
|
||||
return responsex.SystemError(ctx, fmt.Sprintf("系统异常 - %v", err))
|
||||
},
|
||||
})
|
||||
|
||||
app.Use(recover2.New(recover2.Config{
|
||||
EnableStackTrace: true,
|
||||
StackTraceHandler: func(ctx fiber.Ctx, err any) {
|
||||
stack := debug.Stack() // 获取 panic 堆栈
|
||||
|
||||
// 打日志到 zap
|
||||
log.WithContext(ctx.Context()).Errorw("系统堆栈异常",
|
||||
"error", err,
|
||||
"stack", string(stack),
|
||||
)
|
||||
},
|
||||
}), requestid.New(), middleware.Logger())
|
||||
|
||||
router.SetupRouters(app)
|
||||
|
||||
|
||||
Reference in new issue
Block a user