-
Notifications
You must be signed in to change notification settings - Fork 15
/
router.go
227 lines (209 loc) · 6.32 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
// Package goexpress /router returns instance for express Router
// Functions defined here are extended by express.go itself
//
// Express Router takes the url regex as similar to the js one
// Router.Get("/:param") will return the param in Response.Params["param"]
package goexpress
import (
"regexp"
)
// NextFunc is an extension type to help loop of lookup in express.go
type NextFunc func(NextFunc)
// Middleware function singature type
type Middleware func(request Request, response Response)
// A Route contains a regexp and a Router.Middleware type handler
type Route struct {
regex *regexp.Regexp
handler Middleware
isMiddleware bool
}
// router is a Collection of all method types routers
type router struct {
routes map[string][]*Route
}
func newRouter() *router {
r := &router{}
r.routes = make(map[string][]*Route)
r.routes["get"] = []*Route{}
r.routes["post"] = []*Route{}
r.routes["put"] = []*Route{}
r.routes["delete"] = []*Route{}
r.routes["patch"] = []*Route{}
r.routes["options"] = []*Route{}
return r
}
func (r *router) addHandler(method string, isMiddleware bool, url *regexp.Regexp, middleware Middleware) {
var route = &Route{}
route.regex = url
route.handler = middleware
route.isMiddleware = isMiddleware
r.routes[method] = append(r.routes[method], route)
}
// Get function
func (r *router) Get(url string, middleware Middleware) Router {
r.addHandler("get", false, CompileRegex(url), middleware)
return r
}
// Post function
func (r *router) Post(url string, middleware Middleware) Router {
r.addHandler("post", false, CompileRegex(url), middleware)
return r
}
// Put function
func (r *router) Put(url string, middleware Middleware) Router {
r.addHandler("put", false, CompileRegex(url), middleware)
return r
}
// Patch function
func (r *router) Patch(url string, middleware Middleware) Router {
r.addHandler("patch", false, CompileRegex(url), middleware)
return r
}
// Delete function
func (r *router) Delete(url string, middleware Middleware) Router {
r.addHandler("delete", false, CompileRegex(url), middleware)
return r
}
// Options function
func (r *router) Options(url string, middleware Middleware) Router {
r.addHandler("options", false, CompileRegex(url), middleware)
return r
}
// Use can take a function or a new express.Router() instance as argument
func (r *router) Use(middleware interface{}) Router {
// check if its another instance of the router
router, ok := middleware.(Router)
if ok {
r.useRouter(router)
} else {
mware, ok := middleware.(func(request Request, response Response))
if ok {
var regex = CompileRegex("(.*)")
// A middleware is for all type of routes
r.addHandler("get", true, regex, mware)
r.addHandler("post", true, regex, mware)
r.addHandler("put", true, regex, mware)
r.addHandler("patch", true, regex, mware)
r.addHandler("delete", true, regex, mware)
r.addHandler("options", true, regex, mware)
} else {
panic("express.Router.Use can only take a function or a Router instance")
}
}
return r
}
func (r *router) useRouter(router Router) *router {
routes := router.GetRoutes()
for routeType, list := range routes {
if r.routes[routeType] == nil {
r.routes[routeType] = []*Route{}
}
r.routes[routeType] = append(r.routes[routeType], list...)
}
return r
}
func (r *router) GetRoutes() map[string][]*Route {
return r.routes
}
// FindNext finds the suitable router for given url and method
// It returns the middleware if found and a cursor index of array
func (r *router) FindNext(index int, method string, url string, request *request) (Middleware, int, bool) {
var i = index
for i < len(r.routes[method]) {
var route = r.routes[method][i]
if route.regex.MatchString(url) {
var regex = route.regex.FindStringSubmatch(url)
for i, name := range route.regex.SubexpNames() {
if name != "" {
request.params.Set(name, regex[i])
}
}
return route.handler, i, route.isMiddleware
}
i++
}
return nil, -1, false
}
// CompileRegex is a Helper which returns a golang RegExp for a given express route string
func CompileRegex(url string) *regexp.Regexp {
var i = 0
var buffer = "/"
var regexStr = "^"
var endVariable = ">(?:[A-Za-z0-9\\-\\_\\$\\.\\+\\!\\*\\'\\(\\)\\,]+))"
if url[0] == '/' {
i++
}
for i < len(url) {
if url[i] == '/' {
// this is a new group parse the last part
regexStr += buffer + "/"
buffer = ""
i++
} else {
if url[i] == ':' && ((i-1 > 0 && url[i-1] == '/') || (i-1 == -1) || (i-1 >= 0)) {
// a variable found, lets read it
var tempbuffer = "(?P<"
var variableName = ""
var variableNameDone = false
var done = false
var hasRegex = false
var innerGroup = 0
// lets branch in to look deeper
i++
for done != true && i < len(url) {
if url[i] == '/' {
if variableName != "" {
if innerGroup == 0 {
if hasRegex == false {
tempbuffer += endVariable
}
done = true
break
}
}
tempbuffer = ""
break
} else if url[i] == '(' {
if variableNameDone == false {
variableNameDone = true
tempbuffer += ">"
hasRegex = true
}
tempbuffer += string(url[i])
if url[i-1] != '\\' {
innerGroup++
}
} else if url[i] == ')' {
tempbuffer += string(url[i])
if url[i-1] != '\\' {
innerGroup--
}
} else {
if variableNameDone == false {
variableName += string(url[i])
}
tempbuffer += string(url[i])
}
i++
}
if tempbuffer != "" {
if hasRegex == false && done == false {
tempbuffer += endVariable
} else if hasRegex {
tempbuffer += ")"
}
buffer += tempbuffer
} else {
panic("Invalid Route regex")
}
} else {
buffer += string(url[i])
i++
}
}
}
if buffer != "" {
regexStr += buffer
}
return regexp.MustCompile(regexStr + "(?:[\\/]{0,1})$")
}