From a13f32ae0249adba5d89d568ad40f4ed1024f97a Mon Sep 17 00:00:00 2001 From: iuR <2356665641@qq.com> Date: Wed, 14 Dec 2022 14:51:19 +0800 Subject: [PATCH] add gin.middleware --- gin/middleware/cors.go | 30 ++++++++++++++++++++++++++++++ gin/middleware/param.go | 31 +++++++++++++++++++++++++++++++ gin/middleware/recover.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 gin/middleware/cors.go create mode 100644 gin/middleware/param.go create mode 100644 gin/middleware/recover.go diff --git a/gin/middleware/cors.go b/gin/middleware/cors.go new file mode 100644 index 0000000..c8c8b83 --- /dev/null +++ b/gin/middleware/cors.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func Cors(url ...string) gin.HandlerFunc { + urlStr := "*" + if len(url) > 0 { + urlStr = url[0] + } + + return func(c *gin.Context) { + method := c.Request.Method + origin := c.Request.Header.Get("Origin") + if origin != "" { + c.Header("AccessControl-Allow-Origin", urlStr) // 可将将 * 替换为指定的域名 + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, UT, DELETE, UPDATE") + c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Acept, Authorization") + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Alow-Headers, Cache-Control, Content-Language, Content-Type") + c.Header("Access-Control-Allow-Credentials", "true") + } + if method == "OPTIONS" { + c.AbortWithStatus(http.StatusNoContent) + } + c.Next() + } +} diff --git a/gin/middleware/param.go b/gin/middleware/param.go new file mode 100644 index 0000000..502821d --- /dev/null +++ b/gin/middleware/param.go @@ -0,0 +1,31 @@ +package middleware + +import ( + "errors" + "reflect" + + "github.com/go-playground/validator/v10" +) + +// 自定义错误消息 +func FilterBindErr(errs error, r any) error { + vErrs := errs.(validator.ValidationErrors) + s := reflect.TypeOf(r) + for _, fieldError := range vErrs { + filed, _ := s.FieldByName(fieldError.Field()) //用反射获取参数名 + + //获取对应binding得错误消息 tag:错误类型_err,如:required_err:"userId不能为空"` + if errTagText := filed.Tag.Get(fieldError.Tag() + "_err"); errTagText != "" { + return errors.New(errTagText) + } + + //通用错误类型 tag:err,如:err:"userId错误"` + if errText := filed.Tag.Get("err"); errText != "" { + return errors.New(errText) + } + + //无法匹配到错误,如:user的格式需遵守: required + return errors.New(fieldError.Field() + "的格式需遵守: " + fieldError.Tag()) + } + return errors.New("") +} diff --git a/gin/middleware/recover.go b/gin/middleware/recover.go new file mode 100644 index 0000000..f7bc0ad --- /dev/null +++ b/gin/middleware/recover.go @@ -0,0 +1,32 @@ +package middleware + +import ( + "fmt" + "log" + "runtime/debug" + + "github.com/gin-gonic/gin" +) + +func Recover(stack bool) gin.HandlerFunc { + return func(c *gin.Context) { + defer func() { + if r := recover(); r != nil { + var errStr string + if stack { + //打印错误堆栈信息 + log.Printf("panic: %v\n", r) + debug.PrintStack() + + errStr = "服务器内部错误" + fmt.Sprintf("panic: %v\n", r) + } else { + errStr = "服务器内部错误" + } + + //封装通用json返回 + c.JSON(500, gin.H{500, errStr, nil}) + } + }() + c.Next() + } +}