-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrouter.go
426 lines (368 loc) · 13.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package frodo
import (
"fmt"
"log"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
// Router is a http.Handler which can be used to dispatch requests to different
// handler functions via configurable routes
type Router struct {
trees map[string]*node
// Enables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists.
// For example if /foo/ is requested but a route only exists for /foo, the
// client is redirected to /foo with http status code 301 for GET requests
// and 307 for all other request methods.
RedirectTrailingSlash bool
// If enabled, the router tries to fix the current request path, if no
// handle is registered for it.
// First superfluous path elements like ../ or // are removed.
// Afterwards the router does a case-insensitive lookup of the cleaned path.
// If a handle can be found for this route, the router makes a redirection
// to the corrected path with status code 301 for GET requests and 307 for
// all other request methods.
// For example /FOO and /..//Foo could be redirected to /foo.
// RedirectTrailingSlash is independent of this option.
RedirectFixedPath bool
// If enabled, the router checks if another method is allowed for the
// current route, if the current request can not be routed.
// If this is the case, the request is answered with 'Method Not Allowed'
// and HTTP status code 405.
// If no other Method is allowed, the request is delegated to the NotFound
// handler.
HandleMethodNotAllowed bool
// Configurable http.Handler which is called when no matching route is
// found. If it is not set, http.NotFound is used.
NotFoundHandler Handler
// Configurable http.Handler which is called when a request
// cannot be routed and HandleMethodNotAllowed is true.
// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
MethodNotAllowedHandler Handler
// Function to handle panics recovered from http handlers.
// It should be used to generate a error page and return the http error code
// 500 (Internal Server Error).
// The handler can be used to keep your server from crashing because of
// unrecovered panics.
PanicHandler Handler
}
// Make sure the Router conforms with the http.Handler interface
var _ http.Handler = New()
// Get is a shortcut for router.Handle("GET", path, handle)
func (r *Router) Get(path string, handlers ...interface{}) {
r.Handle("GET", path, handlers...)
}
// Head is a shortcut for router.Handle("HEAD", path, ...handle)
func (r *Router) Head(path string, handlers ...interface{}) {
r.Handle("HEAD", path, handlers...)
}
// Options is a shortcut for router.Handle("OPTIONS", path, ...handle)
func (r *Router) Options(path string, handlers ...interface{}) {
r.Handle("OPTIONS", path, handlers...)
}
// Post is a shortcut for router.Handle("POST", path, ...handle)
func (r *Router) Post(path string, handlers ...interface{}) {
r.Handle("POST", path, handlers...)
}
// Put is a shortcut for router.Handle("PUT", path, ...handle)
func (r *Router) Put(path string, handlers ...interface{}) {
r.Handle("PUT", path, handlers...)
}
// Patch is a shortcut for router.Handle("PATCH", path, ...handle)
func (r *Router) Patch(path string, handlers ...interface{}) {
r.Handle("PATCH", path, handlers...)
}
// Delete is a shortcut for router.Handle("DELETE", path, ...handle)
func (r *Router) Delete(path string, handlers ...interface{}) {
r.Handle("DELETE", path, handlers...)
}
// Match adds the Handle to the provided Methods/HTTPVerbs for a given route
// EG. GET/POST from /home to have the same Handle
func (r *Router) Match(httpVerbs Methods, path string, handlers ...interface{}) {
if len(httpVerbs) > 0 {
for _, verb := range httpVerbs {
r.Handle(strings.ToUpper(verb), path, handlers...)
}
}
}
// Any method adds the Handle to all HTTP methods/HTTP verbs for the route given
// it does not add routing Handlers for HEADER and OPTIONS HTTP verbs
func (r *Router) Any(path string, handlers ...interface{}) {
r.Match(Methods{"GET", "POST", "PUT", "DELETE", "PATCH"}, path, handlers...)
}
// Handle registers a new request handle with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (r *Router) Handle(method, path string, handlers ...interface{}) {
// Two things satisfy the Middleware interface
// Controller and a Handler
if path[0] != '/' {
panic("path must begin with '/' in path '" + path + "'")
}
// this is used to collect all Handlers
var middleware = make([]Middleware, len(handlers))
for pos, h := range handlers {
// to recieve the typecasted Controller or Handler
var handle Middleware
// Check to see if a Handler was provided if not
v := reflect.ValueOf(h).Type()
fmt.Printf("==> Handler provided: %s\n", v)
// Debug: First of check if it is a Frodo.Handler type
// might have been altered on first/previous loop
// if not check the function if it suffices the Handle type pattern
// If it does -- func(http.ResponseWriter, *Request)
// then convert it to a Frodo.Handler type
if value, isHandler := h.(func(http.ResponseWriter, *Request)); isHandler && v.Kind().String() == "func" {
// morph it to it's dynamic data type
handle = makeHandler(value)
} else {
// It is not a Handler, checked if it is a Controller
if ctrl, isController := h.(CRUDController); isController {
// fmt.Println("converting struct related to Frodo.BaseController")
handle = ctrl
} else {
panic("Error: expected Controller arguement provided to be an extension of " +
"Frodo.BaseController or \"func(http.ResponseWriter, *Frodo.Request)\" type")
}
}
// replace the Middleware with correct Handler
middleware[pos] = handle
}
fmt.Printf("%v and the no %d\n", middleware, len(middleware))
if r.trees == nil {
r.trees = make(map[string]*node)
}
root := r.trees[method]
if root == nil {
root = new(node)
r.trees[method] = root
}
// store them to it's route node
root.addRoute(path, middleware)
}
// Handler is an adapter which allows the usage of an
// http.Handler as a request handle.
func (r *Router) Handler(method, path string, handler http.Handler) {
r.Handle(method, path, func(w http.ResponseWriter, req *Request) {
handler.ServeHTTP(w, req.Request)
})
}
// HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a
// request handle.
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) {
r.Handler(method, path, handler)
}
// ServeFiles serves files from the given file system root.
// The path must end with "/*filepath", files are then served from the local
// path /defined/root/dir/*filepath.
// For example if root is "/etc" and *filepath is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// To use the operating system's file system implementation,
// use http.Dir:
// router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (r *Router) ServeFiles(path string, root http.FileSystem) {
if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
panic("path must end with /*filepath in path '" + path + "'")
}
fileServer := http.FileServer(root)
r.Get(path, func(w http.ResponseWriter, req *Request) {
req.URL.Path = req.GetParam("filepath")
fileServer.ServeHTTP(w, req.Request)
})
}
// NotFound can be used to define custom routes to handle NotFound routes
func (r *Router) NotFound(handler Handler) {
r.NotFoundHandler = handler
}
// MethodNotAllowed can be used to define custom routes
// to handle Methods that are not allowed
func (r *Router) MethodNotAllowed(handler Handler) {
r.MethodNotAllowedHandler = handler
}
// ServerError can be used to define custom routes to handle OnServerError routes
func (r *Router) ServerError(handler Handler) {
r.PanicHandler = handler
}
// On404 is shortform for NotFound
func (r *Router) On404(handler Handler) {
r.NotFound(handler)
}
// On405 is shortform for NotFound
func (r *Router) On405(handler Handler) {
r.MethodNotAllowed(handler)
}
// On500 is shortform for ServerError
func (r *Router) On500(handler Handler) {
r.ServerError(handler)
}
func (r *Router) recover(w *ResponseWriter, req *Request) {
if err := recover(); err != nil {
// if a custom panic handler has been defined
// run that instead
if r.PanicHandler != nil {
r.PanicHandler(w, req)
return
}
// If it doesnt, use original http error function as fallback
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
// Lookup allows the manual lookup of a method + path combo.
// This is e.g. useful to build a framework around this router.
// If the path was found, it returns the handle function and the path parameter
// values. Otherwise the third return value indicates whether a redirection to
// the same path with an extra / without the trailing slash should be performed.
func (r *Router) Lookup(method, path string) ([]Middleware, Params, bool) {
if root := r.trees[method]; root != nil {
return root.getValue(path)
}
return nil, nil, false
}
// ServeHTTP makes the router implement the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// 1st things 1st, wrap the response writter
// to add the extra functionality we want basically
// trace when a write happens
FrodoWritter := ResponseWriter{
ResponseWriter: w,
timeStart: time.Now(),
method: req.Method,
route: req.URL.Path,
}
// Wrap the supplied http.Request
FrodoRequest := Request{
Request: req,
// files []*UploadFile
}
// ---------- Handle 500: Internal Server Error -----------
// If a panic/error takes place while process,
// recover and run PanicHandle if defined
defer r.recover(&FrodoWritter, &FrodoRequest)
if root := r.trees[req.Method]; root != nil {
path := req.URL.Path
// get the Handle of the route path requested
handlers, ps, tsr := root.getValue(path)
// if []Middleware was found were found, run it!
noOfHandlers := len(handlers)
if noOfHandlers > 0 {
// if the 1st handler is defined, run it
FrodoRequest.Params = ps
FrodoRequest.RequestMiddleware = &RequestMiddleware{
handlers: handlers[:],
total: noOfHandlers,
nextPosition: 0,
ResponseWriter: &FrodoWritter,
Request: &FrodoRequest,
}
// Trigger the middleware chain of handlers to be triggered one by one
// the rest shall be called to run by m.Next()
FrodoRequest.RequestMiddleware.chainReaction()
return
}
// if a handle was not found, the method is not a CONNECT request
// and it is not a root path request
if noOfHandlers == 0 && req.Method != "CONNECT" && path != "/" {
code := 301 // Permanent redirect, request with GET method
if req.Method != "GET" {
// Temporary redirect, request with same method
// As of Go 1.3, Go does not support status code 308.
code = 307
}
if tsr && r.RedirectTrailingSlash {
if len(path) > 1 && path[len(path)-1] == '/' {
req.URL.Path = path[:len(path)-1]
} else {
req.URL.Path = path + "/"
}
http.Redirect(w, req, req.URL.String(), code)
return
}
// Try to fix the request path
if r.RedirectFixedPath {
fixedPath, found := root.findCaseInsensitivePath(
CleanPath(path),
r.RedirectTrailingSlash,
)
if found {
req.URL.Path = string(fixedPath)
http.Redirect(w, req, req.URL.String(), code)
return
}
}
}
}
// Handle 405
if r.HandleMethodNotAllowed {
for method := range r.trees {
// Skip the requested method - we already tried this one
if method == req.Method {
continue
}
handle, ps, _ := r.trees[method].getValue(req.URL.Path)
if handle != nil {
if r.MethodNotAllowedHandler != nil {
FrodoRequest.Params = ps
r.MethodNotAllowedHandler(&FrodoWritter, &FrodoRequest)
return
}
// if no MethodNotAllowedHandler found, just throw an error the old way
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
}
return
}
// Handle 404
if r.NotFoundHandler != nil {
r.NotFoundHandler(&FrodoWritter, &FrodoRequest)
return
}
// If there is not Handle for a 404 error use Go's w
http.Error(w, http.StatusText(404), http.StatusNotFound)
return
}
// Serve deploys the application
// Default port is 3102, inspired by https://en.wikipedia.org/wiki/Fourth_Age
// The "Fourth Age" followed the defeat of Sauron and the destruction of his One Ring,
// but did not officially begin until after the Bearers of the Three Rings left Middle-earth for Valinor,
// the 'Uttermost West'
func (r *Router) Serve() {
r.ServeOnPort(3102)
}
// ServeOnPort is to used if you plan change the port to serve the application on
func (r *Router) ServeOnPort(portNumber interface{}) {
var portNumberAsString string
// Converting an interface into the data type it should be
if pns, ok := portNumber.(int); ok {
portNumberAsString = strconv.Itoa(pns)
} else {
// if it is not a number/int provided then it must be a string
if pns, ok := portNumber.(string); ok {
if pns == "" {
pns = "3102"
}
portNumberAsString = pns
} else {
log.Fatal("[ERROR] PortNumber can only be a numeral string or integer")
return
}
}
err := http.ListenAndServe(":"+portNumberAsString, r)
if err != nil {
log.Fatalf("[ERROR] Server failed to initialise: %s", err)
return
}
// If server successfully Launched
log.Printf("[LOG] Server deployed at: %s", portNumberAsString)
}