From 957ba155df5ed00f7a0919e6dc14b1a883bf91e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Fri, 26 Mar 2021 14:34:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=B3=A8=E5=86=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=85=8D=E7=BD=AE=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/util/responsed.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/gin/util/responsed.go b/gin/util/responsed.go index a093af6..a1bf04d 100644 --- a/gin/util/responsed.go +++ b/gin/util/responsed.go @@ -37,24 +37,27 @@ func Response(ctx *gin.Context, code interface{}, message string, data interface // Author : go_developer@163.com<张德满> // // Date : 8:36 下午 2021/3/9 -func RegisterRouter(router *gin.Engine, method string, uri string, handler gin.HandlerFunc) error { +func RegisterRouter(router *gin.Engine, method string, uri string, handler gin.HandlerFunc, middlewareList []gin.HandlerFunc) error { + if nil == middlewareList { + middlewareList = make([]gin.HandlerFunc, 0) + } switch strings.ToUpper(method) { case http.MethodGet: - router.GET(uri, handler) + router.GET(uri, handler).Use(middlewareList...) case http.MethodPost: - router.POST(uri, handler) + router.POST(uri, handler).Use(middlewareList...) case http.MethodDelete: - router.DELETE(uri, handler) + router.DELETE(uri, handler).Use(middlewareList...) case http.MethodHead: - router.HEAD(uri, handler) + router.HEAD(uri, handler).Use(middlewareList...) case http.MethodOptions: - router.OPTIONS(uri, handler) + router.OPTIONS(uri, handler).Use(middlewareList...) case http.MethodPatch: - router.PATCH(uri, handler) + router.PATCH(uri, handler).Use(middlewareList...) case http.MethodPut: - router.PUT(uri, handler) + router.PUT(uri, handler).Use(middlewareList...) case "ANY": // 一次性注册全部请求方法的路由 - router.Any(uri, handler) + router.Any(uri, handler).Use(middlewareList...) default: // 不是一个函数,数名method配置错误 return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method)