fix : brahma api 中间件的修复

master
liuhaotian 2022-01-14 04:09:44 +08:00
parent be9aac681f
commit 6267dc56f2
2 changed files with 8 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import (
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler"
"git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/api"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid" "github.com/gofiber/fiber/v2/middleware/requestid"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap" "go.uber.org/zap"
@ -22,11 +23,8 @@ const (
var ( var (
err error err error
//Init App
app = fiber.New(fiber.Config{ app = fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error { ErrorHandler: api.FiberMiddleWareErrHandler,
return ctx.Status(fiber.StatusInternalServerError).JSON(handler.Response{Message: "服务器内部错误"})
},
}) })
) )
@ -49,12 +47,10 @@ func ResourceInit() {
} }
func appInit() { func appInit() {
//requestId
app.Use(requestid.New()) app.Use(requestid.New())
//recover
app.Use(api.FibberMiddleWareRecoverHandler)
//requestId for gorm
app.Use(api.FibberUserCtxWithTraceHandler) app.Use(api.FibberUserCtxWithTraceHandler)
app.Use(api.FibberMiddleWareRespHandler)
app.Use(recover.New(api.GetFibberRecoverConfig()))
} }
func routerInit() { func routerInit() {

View File

@ -50,9 +50,9 @@ func FibberMiddleWareRespHandler(c *fiber.Ctx) error {
return nil return nil
} }
// FibberMiddleWareRecoverHandler fiberPanicRecover // GetFibberRecoverConfig fiberPanicRecoverConfig
func FibberMiddleWareRecoverHandler() fiber.Handler { func GetFibberRecoverConfig() recover.Config {
return recover.New(recover.Config{ return recover.Config{
EnableStackTrace: true, EnableStackTrace: true,
StackTraceHandler: func(c *fiber.Ctx, e interface{}) { StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
buf := make([]byte, 1024*1024) buf := make([]byte, 1024*1024)
@ -60,7 +60,7 @@ func FibberMiddleWareRecoverHandler() fiber.Handler {
fmt.Printf("panic: %v\n%s\n", e, buf) fmt.Printf("panic: %v\n%s\n", e, buf)
ctxLogger.FError(c, "panic", zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf))) ctxLogger.FError(c, "panic", zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf)))
}, },
}) }
} }
// FibberUserCtxWithTraceHandler 把fibberCtx的traceId 置入UserCtx中 // FibberUserCtxWithTraceHandler 把fibberCtx的traceId 置入UserCtx中