-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeGroup.go
138 lines (113 loc) · 3.5 KB
/
routeGroup.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package rego
import (
path2 "path"
)
//主要负责将得到路由信息后,将信息存入MethodRouteMaps
//最后通过engine来得到此MethodRouteMaps信息
//且结构体参数初始化在engine中初始化
type RouteGroup struct{
//路由函数链
handlers HandlerChain
//路由路径
path string
//路由匹配Hash
methodRouteMaps MethodRouteMaps
//路由引擎
engine *Engine
}
//添加路由信息相关
func (group *RouteGroup)addRoute(httpMethod,rePath string,handlers HandlerChain){
result:=group.engine.methodRouteMaps.get(httpMethod)
if result==nil{
result=new(RouteMap)
group.engine.methodRouteMaps=append(group.methodRouteMaps,MethodRouteMap{
httpMethod: httpMethod,
RouteMap: result,
})
result.reset()
}
result.addRoute(rePath,handlers)
}
//func (group *RouteGroup)addRoute(httpMethod,rePath string,handlers HandlerChain){
// result:=group.methodRouteMaps.get(httpMethod)
// if result==nil{
// result=new(RouteMaps)
// group.methodRouteMaps=append(group.methodRouteMaps,MethodRouteMap{
// httpMethod: httpMethod,
// routeMap: result,
// })
// }
// result.addRouteMap(rePath,handlers)
//}
//处理路由信息相关
func (group *RouteGroup) Use(middleware ...HandlerFunc) {
group.handlers = append(group.handlers, middleware...)
}
func (group *RouteGroup)Group(path string,handlers ...HandlerFunc)*RouteGroup{
return &RouteGroup{
handlers: group.combineHandlers(handlers),
path: group.absolutePath(path),
methodRouteMaps:group.methodRouteMaps,
engine: group.engine,
}
}
func (group *RouteGroup)handle(httpMethod,path string,handlers HandlerChain){
rePath:=group.absolutePath(path)
reHandlers:=group.combineHandlers(handlers)
group.addRoute(httpMethod,rePath,reHandlers)
}
func (group *RouteGroup)GET(path string,handlers ...HandlerFunc){
group.handle("GET",path,handlers)
}
func (group *RouteGroup)POST(path string,handlers ...HandlerFunc){
group.handle("POST",path,handlers)
}
func (group *RouteGroup) DELETE(path string, handlers ...HandlerFunc) {
group.handle("DELETE", path, handlers)
}
func (group *RouteGroup) PATCH(path string, handlers ...HandlerFunc) {
group.handle("PATCH", path, handlers)
}
func (group *RouteGroup) PUT(path string, handlers ...HandlerFunc) {
group.handle("PUT", path, handlers)
}
func (group *RouteGroup) OPTIONS(path string, handlers ...HandlerFunc) {
group.handle("OPTIONS", path, handlers)
}
func (group *RouteGroup) HEAD(path string, handlers ...HandlerFunc) {
group.handle("HEAD", path, handlers)
}
func (group *RouteGroup) ANY(path string, handlers ...HandlerFunc) {
group.handle("GET", path, handlers)
group.handle("POST", path, handlers)
group.handle("PUT", path, handlers)
group.handle("PATCH", path, handlers)
group.handle("HEAD", path, handlers)
group.handle("OPTIONS", path, handlers)
group.handle("DELETE", path, handlers)
group.handle("CONNECT", path, handlers)
group.handle("TRACE", path, handlers)
}
func (group *RouteGroup)absolutePath(path string)string{
repath:=group.path
if path==""{
return repath
}
finalPath:=path2.Join(repath,path)
pathLen:=len(path)-1
finalPathLen:=len(finalPath)-1
if path[pathLen]=='/'&&finalPath[finalPathLen]!='/'{
return finalPath+"/"
}
return finalPath
}
func (group *RouteGroup)combineHandlers(handlers HandlerChain)HandlerChain{
Size:=len(group.handlers)+len(handlers)
if Size>=int(abortIndex){
PrintErr(" too many handlers")
}
reHandlers:=make(HandlerChain,Size)
copy(reHandlers,group.handlers)
copy(reHandlers[len(group.handlers):],handlers)
return reHandlers
}