From d0fb4a6bf02b1c92231f89220c563175dad381f8 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 18 Jul 2014 00:29:44 +0200 Subject: [PATCH] Fixes new NoRoute() logic --- gin.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/gin.go b/gin.go index 2dd2da68..cbda6096 100644 --- a/gin.go +++ b/gin.go @@ -35,15 +35,16 @@ type ( // Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares. Engine struct { *RouterGroup - HTMLRender render.Render - cache sync.Pool - handlers404 []HandlerFunc - router *httprouter.Router + HTMLRender render.Render + cache sync.Pool + finalNoRoute []HandlerFunc + noRoute []HandlerFunc + router *httprouter.Router } ) func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) { - c := engine.createContext(w, req, nil, engine.handlers404) + c := engine.createContext(w, req, nil, engine.finalNoRoute) c.Writer.setStatus(404) c.Next() if !c.Writer.Written() { @@ -92,7 +93,13 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) { // Adds handlers for NoRoute. It return a 404 code by default. func (engine *Engine) NoRoute(handlers ...HandlerFunc) { - engine.handlers404 = engine.combineHandlers(handlers) + engine.noRoute = handlers + engine.finalNoRoute = engine.combineHandlers(engine.noRoute) +} + +func (engine *Engine) Use(middlewares ...HandlerFunc) { + engine.RouterGroup.Use(middlewares...) + engine.finalNoRoute = engine.combineHandlers(engine.noRoute) } // ServeHTTP makes the router implement the http.Handler interface.