-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutermethod.go
216 lines (195 loc) · 5.9 KB
/
routermethod.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package xmux
import (
"log"
"net/http"
"path"
)
// get this route
func (r *Router) defindMethod(pattern string, handler func(http.ResponseWriter, *http.Request), method string) *Route {
temphead := make(map[string]string)
for k, v := range r.header {
temphead[k] = v
}
tempPages := make(map[string]struct{})
for k := range r.pagekeys {
tempPages[k] = struct{}{}
}
newRoute := &Route{
handle: http.HandlerFunc(handler),
pagekeys: tempPages,
module: r.module.cloneMudule(),
new: true,
responseData: r.responseData,
header: temphead,
delheader: make(map[string]struct{}),
delmodule: make(map[string]struct{}),
delPageKeys: make(map[string]struct{}),
prefixs: make([]string, 0),
delprefix: map[string]struct{}{},
}
prefix := path.Join(r.prefix...)
prefix = path.Join(prefix, pattern)
// 判断是否是正则
url, vars, ok := r.makeRoute(prefix)
newRoute.params = vars
newRoute.url = url
if ok {
// 正则匹配的
if _, ok := r.tpl[url]; ok {
if _, ok := r.tpl[url][method]; ok {
log.Fatal("method : " + method + " duplicate, url: " + url)
}
} else {
r.tpl[url] = make(MethodsRoute)
}
r.tpl[url][method] = newRoute
} else {
// 直接匹配
// 如果存在就判断是否存在method
if _, ok := r.route[url]; ok {
if _, ok := r.route[url][method]; ok {
log.Fatal("method : " + method + " duplicate, url: " + url)
}
} else {
r.route[url] = make(MethodsRoute)
}
r.route[url][method] = newRoute
}
return newRoute
}
func (r *Router) any(pattern string, handler func(http.ResponseWriter, *http.Request), methods ...string) MethodsRoute {
if len(methods) == 0 {
panic(pattern + " have not any methods")
}
temphead := make(map[string]string)
for k, v := range r.header {
temphead[k] = v
}
tempPages := make(map[string]struct{})
for k := range r.pagekeys {
tempPages[k] = struct{}{}
}
newRoute := &Route{
handle: http.HandlerFunc(handler),
pagekeys: tempPages,
module: r.module.cloneMudule(),
new: true,
responseData: r.responseData,
header: temphead,
delheader: make(map[string]struct{}),
delmodule: make(map[string]struct{}),
delPageKeys: make(map[string]struct{}),
prefixs: make([]string, 0),
delprefix: map[string]struct{}{},
}
// 判断是否是正则
prefix := path.Join(r.prefix...)
prefix = path.Join(prefix, pattern)
url, vars, ok := r.makeRoute(prefix)
newRoute.params = vars
newRoute.url = url
if ok {
// 正则匹配的
for _, method := range methods {
if _, ok := r.tpl[url]; ok {
if _, ok := r.tpl[url][method]; ok {
log.Fatal("method : " + method + " duplicate, url: " + url)
}
} else {
r.tpl[url] = make(MethodsRoute)
}
r.tpl[url][method] = newRoute
}
// 如果不存在就创建一个 route
return r.tpl[url]
} else {
// 直接匹配
for _, method := range methods {
// 如果存在就判断是否存在method
if _, ok := r.route[url]; ok {
if _, ok := r.route[url][method]; ok {
log.Fatal("method : " + method + " duplicate, url: " + url)
}
} else {
r.route[url] = make(MethodsRoute)
}
r.route[url][method] = newRoute
}
return r.route[url]
}
}
func (r *Router) Post(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodPost)
}
func (r *Router) Any(pattern string, handler func(http.ResponseWriter, *http.Request)) MethodsRoute {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.any(pattern, handler, http.MethodConnect,
http.MethodDelete, http.MethodGet, http.MethodHead,
http.MethodPatch, http.MethodPost, http.MethodPut, http.MethodTrace,
)
}
// func (mr MethodsRoute) Bind(data interface{}) MethodsRoute {
// for _, route := range mr {
// route.dataSource = data
// }
// return mr
// }
func (r *Router) Get(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodGet)
}
func (r *Router) Request(pattern string, handler func(http.ResponseWriter, *http.Request), methods ...string) MethodsRoute {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.any(pattern, handler, methods...)
}
func (r *Router) Delete(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodDelete)
}
func (r *Router) Head(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodHead)
}
func (r *Router) Options(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodOptions)
}
func (r *Router) Connect(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodConnect)
}
func (r *Router) Patch(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodPatch)
}
func (r *Router) Trace(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodTrace)
}
func (r *Router) Put(pattern string, handler func(http.ResponseWriter, *http.Request)) *Route {
if !r.new {
panic("must be use get router by NewRouter()")
}
return r.defindMethod(pattern, handler, http.MethodPut)
}