-
Notifications
You must be signed in to change notification settings - Fork 8
/
router.go
288 lines (234 loc) · 7.3 KB
/
router.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
package yarf
import (
"errors"
"strings"
)
// Router interface provides the methods used to handle route and GroupRoute objects.
type Router interface {
Match(string, *Context) bool
Dispatch(*Context) error
}
// GroupRouter interface adds methods to work with children routers
type GroupRouter interface {
Router
Add(string, ResourceHandler)
AddGroup(*GroupRoute)
Insert(MiddlewareHandler)
}
// route struct stores the expected route path and the ResourceHandler that handles that route.
type route struct {
path string // Original route
routeParts []string // parsed Route split into parts
handler ResourceHandler // Handler for the route
}
// Route returns a new route object initialized with the provided data.
// Params:
// - url string // The route path to handle
// - h ResourceHandler // The ResourceHandler object that will process the requests to the url.
//
func Route(url string, h ResourceHandler) Router {
return &route{
path: url,
handler: h,
routeParts: prepareURL(url),
}
}
// Match returns true/false indicating if a request URL matches the route and
// sets the Context Params for matching parts in the original route.
// Route matchs are exact, that means, there are not optional parameters.
// To implement optional parameters you can define different routes handled by the same ResourceHandler.
// When a route matches the request URL, this method will parse and fill
// the parameters parsed during the process into the Context object.
func (r *route) Match(url string, c *Context) bool {
requestParts := prepareURL(url)
// YARF router only accepts exact route matches, so check for part count.
// Unless it's a catch-all route
if len(r.routeParts) == 0 || (len(r.routeParts) > 0 && r.routeParts[len(r.routeParts)-1] != "*") {
if len(r.routeParts) != len(requestParts) {
return false
}
}
// check that requestParts matches routeParts
if !matches(r.routeParts, requestParts) {
return false
}
storeParams(c, r.routeParts, requestParts)
return true
}
// Dispatch executes the right ResourceHandler method based on the HTTP request in the Context object.
func (r *route) Dispatch(c *Context) error {
// Method dispatch
switch c.Request.Method {
case "GET":
return r.handler.Get(c)
case "POST":
return r.handler.Post(c)
case "PUT":
return r.handler.Put(c)
case "PATCH":
return r.handler.Patch(c)
case "DELETE":
return r.handler.Delete(c)
case "OPTIONS":
return r.handler.Options(c)
case "HEAD":
return r.handler.Head(c)
case "TRACE":
return r.handler.Trace(c)
case "CONNECT":
return r.handler.Connect(c)
}
// Return method not implemented
return ErrorMethodNotImplemented()
}
// GroupRoute stores routes grouped under a single url prefix.
type GroupRoute struct {
prefix string // The url prefix path for all routes in the group
routeParts []string // parsed Route split into parts
middleware []MiddlewareHandler // Group middleware resources
routes []Router // Group routes
}
// RouteGroup creates a new GroupRoute object and initializes it with the provided url prefix.
// The object implements Router interface to being able to handle groups as routes.
// Groups can be nested into each other,
// so it's possible to add a GroupRoute as a route inside another GroupRoute.
// Includes methods to work with middleware.
func RouteGroup(url string) *GroupRoute {
return &GroupRoute{
prefix: url,
routeParts: prepareURL(url),
}
}
// Match loops through all routes inside the group and find for one that matches the request.
// After a match is found, the route matching is stored into Context.groupDispatch
// to being able to dispatch it directly after a match without looping again.
// Outside the box, works exactly the same as route.Match()
func (g *GroupRoute) Match(url string, c *Context) bool {
urlParts := prepareURL(url)
// check if urlParts matches routeParts
if !matches(g.routeParts, urlParts) {
return false
}
// Remove prefix part form the request URL
rURL := strings.Join(urlParts[len(g.routeParts):], "/")
// Now look for a match inside the routes collection
for _, r := range g.routes {
if r.Match(rURL, c) {
// store the matching Router and params after a match is found
c.groupDispatch = append(c.groupDispatch, r)
storeParams(c, g.routeParts, urlParts)
return true
}
}
return false
}
// Dispatch loops through all routes inside the group and dispatch the one that matches the request.
// Outside the box, works exactly the same as route.Dispatch().
func (g *GroupRoute) Dispatch(c *Context) (err error) {
if len(c.groupDispatch) == 0 {
g.endDispatch(c)
return errors.New("No matching route found")
}
// Pre-dispatch middleware
for _, m := range g.middleware {
// Dispatch
err = m.PreDispatch(c)
if err != nil {
g.endDispatch(c)
return
}
}
// pop, dispatch last route
n := len(c.groupDispatch) - 1
route := c.groupDispatch[n]
c.groupDispatch = c.groupDispatch[:n]
err = route.Dispatch(c)
if err != nil {
g.endDispatch(c)
return
}
// Post-dispatch middleware
for _, m := range g.middleware {
// Dispatch
err = m.PostDispatch(c)
if err != nil {
g.endDispatch(c)
return
}
}
// End dispatch if no errors blocking...
g.endDispatch(c)
// Return success
return
}
func (g *GroupRoute) endDispatch(c *Context) (err error) {
// End dispatch middleware
for _, m := range g.middleware {
e := m.End(c)
if e != nil {
// If there are any error, only return the last to be sure we go through all middlewares.
err = e
}
}
return
}
// Add inserts a new resource with it's associated route into the group object.
func (g *GroupRoute) Add(url string, h ResourceHandler) {
g.routes = append(g.routes, Route(url, h))
}
// AddGroup inserts a GroupRoute into the routes list of the group object.
// This makes possible to nest groups.
func (g *GroupRoute) AddGroup(r *GroupRoute) {
g.routes = append(g.routes, r)
}
// Insert adds a MiddlewareHandler into the middleware list of the group object.
func (g *GroupRoute) Insert(m MiddlewareHandler) {
g.middleware = append(g.middleware, m)
}
// prepareUrl trims leading and trailing slahses, splits url parts, and removes empty parts
func prepareURL(url string) []string {
return removeEmpty(strings.Split(url, "/"))
}
// removeEmpty removes blank strings from parts in one pass, shifting elements
// of the array down, and returns the altered array.
func removeEmpty(parts []string) []string {
x := parts[:0]
for _, p := range parts {
if p != "" {
x = append(x, p)
}
}
return x
}
// matches returns true if requestParts matches routeParts up through len(routeParts)
// ignoring params in routeParts
func matches(routeParts, requestParts []string) bool {
routeCount := len(routeParts)
// Check for catch-all wildcard
if len(routeParts) > 0 && routeParts[len(routeParts)-1] == "*" {
routeCount--
}
if len(requestParts) < routeCount {
return false
}
// Check for part matching, ignoring params and * wildcards
for i, p := range routeParts {
// Skip wildcard
if p == "*" {
continue
}
if p != requestParts[i] && p[0] != ':' {
return false
}
}
return true
}
// storeParams writes parts from requestParts that correspond with param names in
// routeParts into c.Params.
func storeParams(c *Context, routeParts, requestParts []string) {
for i, p := range routeParts {
if p[0] == ':' {
c.Params.Set(p[1:], requestParts[i])
}
}
}