-
Notifications
You must be signed in to change notification settings - Fork 3
/
route.go
executable file
·46 lines (39 loc) · 1.12 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ginKit
import (
"github.com/gin-gonic/gin"
"github.com/richelieu-yang/chimera/v3/src/core/sliceKit"
"github.com/sirupsen/logrus"
)
// BindHandlersToRoute
/*
适用场景: 1个路由,n个Method.
@param methods nil => 接收所有类型method的请求. e.g. http.MethodGet、http.MethodPost
@param handlers 其中的元素不能为nil!!!
*/
func BindHandlersToRoute(group gin.IRoutes, route string, methods []string, handlers ...gin.HandlerFunc) {
if len(handlers) == 0 {
// do nothing
return
}
for i, handler := range handlers {
if handler == nil {
logrus.Panicf("handler(index: %d) == nil", i)
}
}
if len(methods) == 0 {
// (1) any method
group.Any(route, handlers...)
return
}
// (2) 特定 methods
for _, method := range methods {
group.Handle(method, route, handlers...)
}
}
// BindHandlersToRoutes 将相同的多个处理器,注册到多个路由.
func BindHandlersToRoutes(group gin.IRoutes, routes []string, methods []string, handlers ...gin.HandlerFunc) {
routes = sliceKit.Uniq(routes)
for _, route := range routes {
BindHandlersToRoute(group, route, methods, handlers...)
}
}