This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
334 lines (312 loc) · 7.99 KB
/
routing.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package egret
import (
"errors"
"net/url"
"regexp"
"strings"
"github.com/spf13/cast"
)
/*
/users/<id:\d+>
/users/<*path>
router.Path("/users/<id>/", func(){})
*/
type (
// // Handler the main Iris Handler interface.
// Handler interface {
// // Serve handle route
// Serve(ctx *Context)
// }
// HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
HandlerFunc func(c *Context)
ConstraintFunc func(url string, params map[string]string) bool
Zone struct {
path string
node *pathNode
zones []*Zone
parent *Zone
host *Host
router *Router
beforeHandlers map[string][]HandlerFunc
afterHandlers map[string][]HandlerFunc
}
Host struct {
zones []*Zone
path string
regex *regexp.Regexp
constraint ConstraintFunc
router *Router
namedZones map[string]*Zone
tree *pathTree
beforeHandlers map[string][]HandlerFunc
afterHandlers map[string][]HandlerFunc
}
//Router is struct for save router data.
Router struct {
tree *pathTree
zones []*Zone
namedZones map[string]*Zone
hosts []*Host
beforeHandlers map[string][]HandlerFunc
afterHandlers map[string][]HandlerFunc
}
)
const (
regexEscapeChars = ".$^{[(|)*+?\\"
)
var allMethods = strings.Split("GET,POST,DELETE,PUT,PATCH,CONNECT,HEAD,OPTIONS,TRACE", ",")
//Host set router host name.
func (r *Router) Host(path string) *Host {
host := &Host{
path: path,
regex: regexp.MustCompile(strings.Replace(strings.Replace(path, ".", "\\.", -1), "*", ".*", -1)),
zones: []*Zone{},
router: r,
beforeHandlers: make(map[string][]HandlerFunc),
afterHandlers: make(map[string][]HandlerFunc),
}
r.hosts = append(r.hosts, host)
return host
}
func setPresetHandlers(allHandlers map[string][]HandlerFunc, method string, handlers []HandlerFunc) {
ms := getMethods(method)
for _, m := range ms {
if allHandlers[m] == nil {
allHandlers[m] = handlers
} else {
allHandlers[m] = append(allHandlers[m], handlers...)
}
}
}
func (z *Zone) Route(method string, handlers ...HandlerFunc) *Zone {
ms := getMethods(method)
for _, m := range ms {
allHandlers := handlers
for pz := z; pz != nil; {
if pz.beforeHandlers[m] != nil {
allHandlers = append(pz.beforeHandlers[m], allHandlers...)
}
if pz.afterHandlers[m] != nil {
allHandlers = append(allHandlers, pz.afterHandlers[m]...)
}
pz = pz.parent
}
if z.router.beforeHandlers[m] != nil {
allHandlers = append(z.router.beforeHandlers[m], allHandlers...)
}
if z.router.afterHandlers[m] != nil {
allHandlers = append(allHandlers, z.router.afterHandlers[m]...)
}
z.node.handlers[method] = allHandlers
}
return z
}
func getMethods(l string) []string {
ms := []string{}
if l == "*" {
ms = allMethods
} else {
ms = strings.Split(l, ",")
}
return ms
}
func (z *Zone) SetConstraint(c ConstraintFunc) *Zone {
z.node.setConstraint(c)
return z
}
func (z *Zone) SetStrictSlash(value bool) *Zone {
z.node.setStrictSlash(value)
return z
}
func (r *Router) Path(path string, constraint ...ConstraintFunc) *Zone {
// path = normalizePath(path)
zone := &Zone{
path: path,
zones: []*Zone{},
beforeHandlers: make(map[string][]HandlerFunc),
afterHandlers: make(map[string][]HandlerFunc),
}
zone.node, _ = r.tree.add(path)
zone.router = r
r.zones = append(r.zones, zone)
if len(constraint) > 0 {
zone.SetConstraint(constraint[0])
}
return zone
}
func (z *Zone) Path(path string) *Zone {
path = joinPath(z.path, path)
Logger.Info(path)
if z.host != nil {
z.host.tree.add(path)
}
zone := &Zone{
path: path,
router: z.router,
parent: z,
zones: []*Zone{},
beforeHandlers: make(map[string][]HandlerFunc),
afterHandlers: make(map[string][]HandlerFunc),
}
zone.node, _ = z.router.tree.add(path)
z.zones = append(z.zones, zone)
return zone
}
func (d *Host) Match(method string, url *url.URL) (handlers []HandlerFunc, params map[string]string) {
if d.regex.MatchString(url.Host) && (d.constraint == nil || d.constraint(url.String(), params)) {
return d.tree.get(method, url)
}
return nil, nil
}
func (r *Router) Match(method string, url *url.URL) (handlers []HandlerFunc, params map[string]string) {
for _, host := range r.hosts {
handlers, params := host.Match(method, url)
if handlers != nil {
return handlers, params
}
}
return r.tree.get(method, url)
}
//Reverse build url by route name and params.
func (r *Router) Reverse(routeName string, pairsArgs ...map[string]interface{}) (string, error) {
zone, pairs := r.namedZones[routeName], pairsArgs[0]
if zone != nil {
path := ""
for i := 0; i < len(zone.path); i++ {
if zone.path[i] == '<' {
pname, skipReset := "", false
for ; i < len(zone.path); i++ {
if zone.path[i] == '>' {
break
}
if skipReset {
continue
}
if zone.path[i] == ':' {
skipReset = true
}
if !skipReset {
pname += zone.path
}
}
if pairs[pname] != nil {
path += cast.ToString(pairs[pname])
} else {
return "", errors.New("Missing argument: " + pname)
}
} else {
path += string(zone.path[i])
}
}
return path, nil
}
return "", errors.New("Not found!")
}
func (z *Zone) Name(name string) *Zone {
namedZones := z.router.namedZones
if z.host != nil {
namedZones = z.host.namedZones
}
if namedZones[name] != nil {
Logger.Error("Named path already setted: " + name)
}
namedZones[name] = z
return z
}
func (r *Router) Before(method string, handlers ...HandlerFunc) *Router {
setPresetHandlers(r.beforeHandlers, method, handlers)
return r
}
func (r *Router) After(method string, handlers ...HandlerFunc) *Router {
setPresetHandlers(r.afterHandlers, method, handlers)
return r
}
func (z *Zone) Before(method string, handlers ...HandlerFunc) *Zone {
setPresetHandlers(z.beforeHandlers, method, handlers)
return z
}
func (z *Zone) After(method string, handlers ...HandlerFunc) *Zone {
setPresetHandlers(z.afterHandlers, method, handlers)
return z
}
func (z *Zone) Any(handlers ...HandlerFunc) *Zone {
z.Route("*", handlers...)
return z
}
func (z *Zone) Get(handlers ...HandlerFunc) *Zone {
z.Route("GET", handlers...)
return z
}
func (z *Zone) Post(handlers ...HandlerFunc) *Zone {
z.Route("POST", handlers...)
return z
}
func (z *Zone) Delete(handlers ...HandlerFunc) *Zone {
z.Route("DELETE", handlers...)
return z
}
func (z *Zone) Put(handlers ...HandlerFunc) *Zone {
z.Route("PUT", handlers...)
return z
}
func (z *Zone) Patch(handlers ...HandlerFunc) *Zone {
z.Route("PATCH", handlers...)
return z
}
func (z *Zone) Connect(handlers ...HandlerFunc) *Zone {
z.Route("CONNECT", handlers...)
return z
}
func (z *Zone) Head(handlers ...HandlerFunc) *Zone {
z.Route("HEAD", handlers...)
return z
}
func (z *Zone) Options(handlers ...HandlerFunc) *Zone {
z.Route("OPTIONS", handlers...)
return z
}
func (z *Zone) Trace(handlers ...HandlerFunc) *Zone {
z.Route("TRACE", handlers...)
return z
}
func joinPath(args ...string) string {
path := ""
for _, arg := range args {
if path != "" && path[len(path)-1] == '/' {
if arg[0] == '/' {
path += arg[1:]
} else {
path += arg
}
} else {
if arg[0] == '/' {
path += arg
} else {
path += "/" + arg
}
}
}
return path
}
var routers = []*Router{}
func NewRouter() *Router {
router := &Router{
tree: newPathTree(),
namedZones: make(map[string]*Zone),
beforeHandlers: make(map[string][]HandlerFunc),
afterHandlers: make(map[string][]HandlerFunc),
}
routers = append(routers, router)
return router
}
func ReverseURL(name string, pairs ...map[string]interface{}) (string, error) {
for _, router := range routers {
if url, err := router.Reverse(name, pairs...); err == nil {
return url, nil
}
}
return "", errors.New("Not found valid named route")
}