From f4d09801ff956409f49aa5c60b2de843eb0778a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Tue, 9 Mar 2021 20:37:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E5=86=8Cgin?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/util/responsed.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/gin/util/responsed.go b/gin/util/responsed.go index 773a7ac..3b06c5e 100644 --- a/gin/util/responsed.go +++ b/gin/util/responsed.go @@ -8,7 +8,9 @@ package util import ( + "fmt" "net/http" + "strings" "time" "github.com/gin-gonic/gin" @@ -29,3 +31,33 @@ func Response(ctx *gin.Context, code interface{}, message string, data interface } ctx.JSON(http.StatusOK, responseData) } + +// RegisterRouter 注册gin路由 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:36 下午 2021/3/9 +func RegisterRouter(router *gin.Engine, method string, uri string, handler gin.HandlerFunc) error { + switch strings.ToUpper(method) { + case http.MethodGet: + router.GET(uri, handler) + case http.MethodPost: + router.POST(uri, handler) + case http.MethodDelete: + router.DELETE(uri, handler) + case http.MethodHead: + router.HEAD(uri, handler) + case http.MethodOptions: + router.OPTIONS(uri, handler) + case http.MethodPatch: + router.PATCH(uri, handler) + case http.MethodPut: + router.PUT(uri, handler) + case "ANY": // 一次性注册全部请求方法的路由 + router.Any(uri, handler) + default: + // 不是一个函数,数名method配置错误 + return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method) + } + return nil +}