From ad95c1be6766c068ce697891dddcfca0c9273509 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Thu, 21 Mar 2024 20:18:22 +0800 Subject: [PATCH] recover --- app/mix/controller/init.go | 2 -- library/httpengine/httpengine.go | 1 + library/middleware/recover.go | 12 ++++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 library/middleware/recover.go diff --git a/app/mix/controller/init.go b/app/mix/controller/init.go index ef1c4f7d..2f53f076 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -481,8 +481,6 @@ func ReplyJsonError(ctx *gin.Context, code int, msg string) { } func PrepareOp() gin.HandlerFunc { - defer logger.Recover() - return func(ctx *gin.Context) { //if ctx.ClientIP() != "127.0.0.1" { // ReplyJsonError(ctx, http.StatusBadRequest, fmt.Sprintf("not localhost, ip: %s", ctx.ClientIP())) diff --git a/library/httpengine/httpengine.go b/library/httpengine/httpengine.go index 4c468832..8158c770 100644 --- a/library/httpengine/httpengine.go +++ b/library/httpengine/httpengine.go @@ -10,6 +10,7 @@ func NewRouter() *gin.Engine { router := gin.New() router.Use(middleware.LoggerMiddleware(gin.LoggerConfig{})) + router.Use(middleware.RecoverMiddleware()) return router } diff --git a/library/middleware/recover.go b/library/middleware/recover.go new file mode 100644 index 00000000..d66b0d36 --- /dev/null +++ b/library/middleware/recover.go @@ -0,0 +1,12 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "service/library/logger" +) + +func RecoverMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + defer logger.Recover() + } +}