1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Making Echo#Pre actually work.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-17 13:24:52 -07:00
parent 6f0a7372fb
commit e159f4d7ef
4 changed files with 38 additions and 47 deletions

25
echo.go
View File

@ -192,15 +192,11 @@ func New() (e *Echo) {
} }
e.router = NewRouter(e) e.router = NewRouter(e)
e.middleware = []Middleware{e.router} e.middleware = []Middleware{e.router}
e.head = e.router.Handle(HandlerFunc(func(c Context) error { e.head = HandlerFunc(func(c Context) error {
return c.Handle(c) return c.Handle(c)
})) })
e.pristineHead = e.head e.pristineHead = e.head
e.chainMiddleware()
// e.head = HandlerFunc(func(c Context) error {
// return c.Handle(c)
// })
// e.router.Handle(e.head)
//---------- //----------
// Defaults // Defaults
@ -294,21 +290,18 @@ func (e *Echo) Debug() bool {
// Pre adds middleware to the chain which is run before router. // Pre adds middleware to the chain which is run before router.
func (e *Echo) Pre(middleware ...Middleware) { func (e *Echo) Pre(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...) e.middleware = append(middleware, e.middleware...)
e.head = e.pristineHead e.chainMiddleware()
// Chain middleware
for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head)
}
} }
// Use adds middleware to the chain which is run after router. // Use adds middleware to the chain which is run after router.
func (e *Echo) Use(middleware ...Middleware) { func (e *Echo) Use(middleware ...Middleware) {
e.middleware = append(e.middleware, middleware...) e.middleware = append(e.middleware, middleware...)
e.head = e.pristineHead e.chainMiddleware()
}
// Chain middleware func (e *Echo) chainMiddleware() {
e.head = e.pristineHead
for i := len(e.middleware) - 1; i >= 0; i-- { for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head) e.head = e.middleware[i].Handle(e.head)
} }

View File

@ -83,26 +83,26 @@ func TestEchoMiddleware(t *testing.T) {
e := New() e := New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
e.Use(MiddlewareFunc(func(h Handler) Handler { e.Pre(WrapMiddleware(HandlerFunc(func(c Context) error {
return HandlerFunc(func(c Context) error { assert.Empty(t, c.Path())
buf.WriteString("a") buf.WriteString("-1")
return h.Handle(c) return nil
}) })))
}))
e.Use(MiddlewareFunc(func(h Handler) Handler { e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
return HandlerFunc(func(c Context) error { buf.WriteString("1")
buf.WriteString("b") return nil
return h.Handle(c) })))
})
}))
e.Use(MiddlewareFunc(func(h Handler) Handler { e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
return HandlerFunc(func(c Context) error { buf.WriteString("2")
buf.WriteString("c") return nil
return h.Handle(c) })))
})
})) e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
buf.WriteString("3")
return nil
})))
// Route // Route
e.Get("/", HandlerFunc(func(c Context) error { e.Get("/", HandlerFunc(func(c Context) error {
@ -110,17 +110,18 @@ func TestEchoMiddleware(t *testing.T) {
})) }))
c, b := request(GET, "/", e) c, b := request(GET, "/", e)
assert.Equal(t, "abc", buf.String()) assert.Equal(t, "-1123", buf.String())
assert.Equal(t, http.StatusOK, c) assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "OK", b) assert.Equal(t, "OK", b)
}
// Error func TestEchoMiddlewareError(t *testing.T) {
e.Use(MiddlewareFunc(func(Handler) Handler { e := New()
return HandlerFunc(func(c Context) error { e.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
return errors.New("error") return errors.New("error")
}) })))
})) e.Get("/", notFoundHandler)
c, b = request(GET, "/", e) c, _ := request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c) assert.Equal(t, http.StatusInternalServerError, c)
} }

View File

@ -54,10 +54,7 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
c.Response().Header().Set(echo.ContentEncoding, scheme) c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(g) c.Response().SetWriter(g)
} }
if err := next.Handle(c); err != nil { return next.Handle(c)
c.Error(err)
}
return nil
}) })
} }
} }

View File

@ -39,7 +39,7 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
start := time.Now() start := time.Now()
if err := next.Handle(c); err != nil { if err := next.Handle(c); err != nil {
c.Error(err) return err
} }
stop := time.Now() stop := time.Now()
method := req.Method() method := req.Method()