1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-16 14:26:34 -07:00
parent e16b929900
commit f78c0662c1
2 changed files with 29 additions and 8 deletions

33
echo.go
View File

@ -24,6 +24,7 @@ type (
prefix string prefix string
middleware []Middleware middleware []Middleware
head Handler head Handler
pristineHead Handler
maxParam *int maxParam *int
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler httpErrorHandler HTTPErrorHandler
@ -190,7 +191,16 @@ func New() (e *Echo) {
return NewContext(nil, nil, e) return NewContext(nil, nil, e)
} }
e.router = NewRouter(e) e.router = NewRouter(e)
e.head = e.router.Handle(nil) e.middleware = []Middleware{e.router}
e.head = e.router.Handle(HandlerFunc(func(c Context) error {
return c.Handle(c)
}))
e.pristineHead = e.head
// e.head = HandlerFunc(func(c Context) error {
// return c.Handle(c)
// })
// e.router.Handle(e.head)
//---------- //----------
// Defaults // Defaults
@ -282,14 +292,25 @@ func (e *Echo) Debug() bool {
return e.debug return e.debug
} }
// Use adds handler to the middleware chain. // Pre adds middleware to the chain which is runs before router.
func (e *Echo) Use(middleware ...Middleware) { func (e *Echo) Pre(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...) e.middleware = append(e.middleware, middleware...)
m := append(e.middleware, e.router) e.head = e.pristineHead
// Chain middleware // Chain middleware
for i := len(m) - 1; i >= 0; i-- { for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = m[i].Handle(e.head) e.head = e.middleware[i].Handle(e.head)
}
}
// Use adds middleware to the chain which is runs after router.
func (e *Echo) Use(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...)
e.head = e.pristineHead
// Chain middleware
for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head)
} }
} }

View File

@ -47,12 +47,12 @@ func NewRouter(e *Echo) *Router {
} }
} }
func (r *Router) Handle(h Handler) Handler { func (r *Router) Handle(next Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
method := c.Request().Method() method := c.Request().Method()
path := c.Request().URL().Path() path := c.Request().URL().Path()
r.Find(method, path, c) r.Find(method, path, c)
return c.Handle(c) return next.Handle(c)
}) })
} }