路由注册支持配置中间件

This commit is contained in:
白茶清欢 2021-03-26 14:34:24 +08:00
parent 16156214ed
commit 957ba155df

View File

@ -37,24 +37,27 @@ func Response(ctx *gin.Context, code interface{}, message string, data interface
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 8:36 下午 2021/3/9 // 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) { switch strings.ToUpper(method) {
case http.MethodGet: case http.MethodGet:
router.GET(uri, handler) router.GET(uri, handler).Use(middlewareList...)
case http.MethodPost: case http.MethodPost:
router.POST(uri, handler) router.POST(uri, handler).Use(middlewareList...)
case http.MethodDelete: case http.MethodDelete:
router.DELETE(uri, handler) router.DELETE(uri, handler).Use(middlewareList...)
case http.MethodHead: case http.MethodHead:
router.HEAD(uri, handler) router.HEAD(uri, handler).Use(middlewareList...)
case http.MethodOptions: case http.MethodOptions:
router.OPTIONS(uri, handler) router.OPTIONS(uri, handler).Use(middlewareList...)
case http.MethodPatch: case http.MethodPatch:
router.PATCH(uri, handler) router.PATCH(uri, handler).Use(middlewareList...)
case http.MethodPut: case http.MethodPut:
router.PUT(uri, handler) router.PUT(uri, handler).Use(middlewareList...)
case "ANY": // 一次性注册全部请求方法的路由 case "ANY": // 一次性注册全部请求方法的路由
router.Any(uri, handler) router.Any(uri, handler).Use(middlewareList...)
default: default:
// 不是一个函数,数名method配置错误 // 不是一个函数,数名method配置错误
return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method) return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method)