-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
265 lines (226 loc) · 6.44 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
package router
import (
"context"
"net/http"
"strings"
)
type ctxKey string
var paramsCtxKey = ctxKey("params")
type ops struct {
fn http.HandlerFunc
handler http.Handler
}
// Route is a route
type Route struct {
path string
method string
}
// New creates a new router, allowing for the setup of route handling
func New(path string) Router {
if len(path) == 0 {
path = "/"
}
return Router{
basePath: path,
routes: make(map[Route]*ops),
}
}
// BindContext links the new context with the request to allow for any context values
// to be available later in the chain
func BindContext(c context.Context, r *http.Request) {
*r = *r.WithContext(c)
}
// HaltRequest is most commonly called with the middleware to stop the middleware chain
// from continuing as well as prevent the final handler from being run
func HaltRequest(r *http.Request) {
ctx, cancel := context.WithCancel(r.Context())
BindContext(ctx, r)
cancel()
}
// Params retrieves the url parameters matched
func Params(c context.Context) map[string]string {
switch c.Value(paramsCtxKey).(type) {
case map[string]string:
return c.Value(paramsCtxKey).(map[string]string)
default:
return map[string]string{}
}
}
// Param gets the names url param
func Param(c context.Context, key string) string {
return Params(c)[key]
}
// Router is a custom mux that allows for url parameter to be extracted from the path
type Router struct {
basePath string
routes map[Route]*ops
subRouters []*Router
notFoundHandler http.HandlerFunc
mw []http.HandlerFunc
}
// Before injects the passed in handler functions into the handler chain
func (r *Router) Before(fns ...http.HandlerFunc) {
r.mw = append(r.mw, fns...)
}
// Run executes the handler chain, followed by the final http handler passed in
func (r Router) run(last http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
for _, fn := range r.mw {
fn(w, req)
if req.Context().Err() != nil {
return
}
}
last(w, req)
}
}
func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
method := getMethod(req)
rr := r.findMatchingRouter(req.URL.Path)
for route, ops := range rr.routes {
path := strings.Replace(req.URL.Path, rr.basePath, "", 1)
if ok, params := matches(rr, route, method, path, ops.handler != nil); ok {
var handler http.HandlerFunc
if ops.fn != nil {
handler = ops.fn
} else if ops.handler != nil {
handler = ops.handler.ServeHTTP
}
rr.Before(setURLParams(req, params))
rr.run(handler)(w, req)
return
}
}
w.WriteHeader(http.StatusNotFound)
if r.notFoundHandler != nil {
r.notFoundHandler(w, req)
}
}
// HandleFunc allows the handler to be called when the path matches the request's url path
func (r Router) HandleFunc(method, path string, fn http.HandlerFunc) {
r.bindRoute(method, path, &ops{fn: fn})
}
// SubRouter creates a child router with a custom base path
func (r *Router) SubRouter(path string) *Router {
var basePath string
if r.basePath != "/" {
basePath = r.basePath
}
sub := Router{
basePath: basePath + path,
routes: make(map[Route]*ops),
}
r.subRouters = append(r.subRouters, &sub)
return &sub
}
func (r Router) bindRoute(method, path string, p *ops) {
r.routes[Route{method: method, path: path}] = p
}
// Get handles GET requests
func (r Router) Get(path string, fn http.HandlerFunc) {
r.bindRoute(http.MethodGet, path, &ops{fn: fn})
}
// Post handles POST requests
func (r Router) Post(path string, fn http.HandlerFunc) {
r.bindRoute(http.MethodPost, path, &ops{fn: fn})
}
// Put handles PUT requests
func (r Router) Put(path string, fn http.HandlerFunc) {
r.bindRoute(http.MethodPut, path, &ops{fn: fn})
}
// Delete handles DELETE requests
func (r Router) Delete(path string, fn http.HandlerFunc) {
r.bindRoute(http.MethodDelete, path, &ops{fn: fn})
}
// Patch handles PATCH requests
func (r Router) Patch(path string, fn http.HandlerFunc) {
r.bindRoute(http.MethodPatch, path, &ops{fn: fn})
}
// Handle foo
func (r Router) Handle(path string, h http.Handler) {
r.routes[Route{path: path}] = &ops{handler: h}
}
// NotFound allows for a custom 404 handler to be set
func (r *Router) NotFound(h http.HandlerFunc) {
r.notFoundHandler = h
}
// Finds the matching router
func (r Router) findMatchingRouter(urlPath string) *Router {
for _, child := range r.subRouters {
if r := child.findMatchingRouter(urlPath); r != nil {
return r
}
}
if strings.Index(urlPath, r.basePath) == 0 {
return &r
}
return nil
}
// method returns either the request's overridden method value if it exists or the original method value
func getMethod(r *http.Request) string {
switch r.Header.Get("Content-Type") {
case "multipart/form-data":
r.ParseMultipartForm(10 << 20)
case "text/html":
r.ParseForm()
default:
return r.Method
}
if method := r.FormValue("_method"); method != "" {
return strings.ToUpper(method)
}
return r.Method
}
func matches(router *Router, route Route, method, path string, ignoreMethod bool) (bool, map[string]string) {
routePath := strings.Replace(route.path, router.basePath, "", 1)
if strings.Index(routePath, "/") != 0 {
routePath = "/" + routePath
}
if !ignoreMethod && route.method != method {
return false, nil
}
wildcard := strings.Contains(routePath, "*")
if !wildcard && !strings.Contains(routePath, ":") {
return strings.Trim(routePath, "/") == strings.Trim(path, "/"), nil
}
pathParts, patternParts := slicePath(path), slicePath(routePath)
if wildcard {
if len(pathParts) < len(patternParts) {
return false, nil
}
return true, map[string]string{
"*": strings.Join(pathParts[len(patternParts)-1:], "/"),
}
}
patternPartCount, pathPartCount := len(patternParts), len(pathParts)
if pathPartCount != patternPartCount {
return false, nil
}
// check parts
for i := 0; i < patternPartCount; i++ {
pathPart, patternPart := pathParts[i], patternParts[i]
if patternPart[0] == ':' {
continue
}
if pathPart != patternPart {
return false, nil
}
}
// extract pattern params
params := make(map[string]string)
for i, part := range patternParts {
if part[0] == ':' {
params[part[1:]] = pathParts[i]
}
}
return true, params
}
func slicePath(path string) []string {
return strings.Split(strings.Trim(path, "/"), "/")
}
func setURLParams(r *http.Request, params map[string]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := context.WithValue(r.Context(), paramsCtxKey, params)
*r = *r.WithContext(c)
}
}