diff options
Diffstat (limited to 'vendor/gopkg.in/macaron.v1/router.go')
-rw-r--r-- | vendor/gopkg.in/macaron.v1/router.go | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/vendor/gopkg.in/macaron.v1/router.go b/vendor/gopkg.in/macaron.v1/router.go index befa55f476..950c5bcb09 100644 --- a/vendor/gopkg.in/macaron.v1/router.go +++ b/vendor/gopkg.in/macaron.v1/router.go @@ -82,6 +82,9 @@ type Router struct { groups []group notFound http.HandlerFunc internalServerError func(*Context, error) + + // handlerWrapper is used to wrap arbitrary function from Handler to inject.FastInvoker. + handlerWrapper func(Handler) Handler } func NewRouter() *Router { @@ -115,7 +118,7 @@ func (r *Route) Name(name string) { if len(name) == 0 { panic("route name cannot be empty") } else if r.router.namedRoutes[name] != nil { - panic("route with given name already exists") + panic("route with given name already exists: " + name) } r.router.namedRoutes[name] = r.leaf } @@ -173,7 +176,7 @@ func (r *Router) Handle(method string, pattern string, handlers []Handler) *Rout h = append(h, handlers...) handlers = h } - validateHandlers(handlers) + handlers = validateAndWrapHandlers(handlers, r.handlerWrapper) return r.handle(method, pattern, func(resp http.ResponseWriter, req *http.Request, params Params) { c := r.m.createContext(resp, req) @@ -251,11 +254,11 @@ func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter { return &ComboRouter{r, pattern, h, map[string]bool{}, nil} } -// Configurable http.HandlerFunc which is called when no matching route is +// NotFound configurates http.HandlerFunc which is called when no matching route is // found. If it is not set, http.NotFound is used. // Be sure to set 404 response code in your handler. func (r *Router) NotFound(handlers ...Handler) { - validateHandlers(handlers) + handlers = validateAndWrapHandlers(handlers) r.notFound = func(rw http.ResponseWriter, req *http.Request) { c := r.m.createContext(rw, req) c.handlers = make([]Handler, 0, len(r.m.handlers)+len(handlers)) @@ -265,11 +268,11 @@ func (r *Router) NotFound(handlers ...Handler) { } } -// Configurable handler which is called when route handler returns +// InternalServerError configurates handler which is called when route handler returns // error. If it is not set, default handler is used. // Be sure to set 500 response code in your handler. func (r *Router) InternalServerError(handlers ...Handler) { - validateHandlers(handlers) + handlers = validateAndWrapHandlers(handlers) r.internalServerError = func(c *Context, err error) { c.index = 0 c.handlers = handlers @@ -278,9 +281,21 @@ func (r *Router) InternalServerError(handlers ...Handler) { } } +// SetHandlerWrapper sets handlerWrapper for the router. +func (r *Router) SetHandlerWrapper(f func(Handler) Handler) { + r.handlerWrapper = f +} + func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if t, ok := r.routers[req.Method]; ok { - h, p, ok := t.Match(req.URL.Path) + // Fast match for static routes + leaf := r.getLeaf(req.Method, req.URL.Path) + if leaf != nil { + leaf.handle(rw, req, nil) + return + } + + h, p, ok := t.Match(req.URL.EscapedPath()) if ok { if splat, ok := p["*0"]; ok { p["*"] = splat // Easy name. |