1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

- Handler and middleware signature changed

- Pre chain middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-02-15 17:12:15 -08:00 committed by Vishal Rana
parent 51acf465fe
commit caf3bd31eb
5 changed files with 100 additions and 137 deletions

120
echo.go
View File

@ -23,6 +23,7 @@ type (
Echo struct { Echo struct {
prefix string prefix string
middleware []Middleware middleware []Middleware
head Handler
http2 bool http2 bool
maxParam *int maxParam *int
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
@ -193,7 +194,7 @@ func New() (e *Echo) {
return NewContext(nil, nil, e) return NewContext(nil, nil, e)
} }
e.router = NewRouter(e) e.router = NewRouter(e)
e.middleware = []Middleware{e.router} e.head = e.router.Handle(nil)
//---------- //----------
// Defaults // Defaults
@ -284,80 +285,85 @@ func (e *Echo) Debug() bool {
} }
// Use adds handler to the middleware chain. // Use adds handler to the middleware chain.
func (e *Echo) Use(middleware ...interface{}) { func (e *Echo) Use(middleware ...Middleware) {
for _, m := range middleware { e.middleware = append(e.middleware, middleware...)
e.middleware = append(e.middleware, wrapMiddleware(m)) m := append(e.middleware, e.router)
// Chain middleware
for i := len(m) - 1; i >= 0; i-- {
e.head = m[i].Handle(e.head)
} }
} }
// Connect adds a CONNECT route > handler to the router. // Connect adds a CONNECT route > handler to the router.
func (e *Echo) Connect(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Connect(path string, h Handler, m ...Middleware) {
e.add(CONNECT, path, handler, middleware...) e.add(CONNECT, path, h, m...)
} }
// Delete adds a DELETE route > handler to the router. // Delete adds a DELETE route > handler to the router.
func (e *Echo) Delete(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Delete(path string, h Handler, m ...Middleware) {
e.add(DELETE, path, handler, middleware...) e.add(DELETE, path, h, m...)
} }
// Get adds a GET route > handler to the router. // Get adds a GET route > handler to the router.
func (e *Echo) Get(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Get(path string, h Handler, m ...Middleware) {
e.add(GET, path, handler, middleware...) e.add(GET, path, h, m...)
} }
// Head adds a HEAD route > handler to the router. // Head adds a HEAD route > handler to the router.
func (e *Echo) Head(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Head(path string, h Handler, m ...Middleware) {
e.add(HEAD, path, handler, middleware...) e.add(HEAD, path, h, m...)
} }
// Options adds an OPTIONS route > handler to the router. // Options adds an OPTIONS route > handler to the router.
func (e *Echo) Options(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Options(path string, h Handler, m ...Middleware) {
e.add(OPTIONS, path, handler, middleware...) e.add(OPTIONS, path, h, m...)
} }
// Patch adds a PATCH route > handler to the router. // Patch adds a PATCH route > handler to the router.
func (e *Echo) Patch(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Patch(path string, h Handler, m ...Middleware) {
e.add(PATCH, path, handler, middleware...) e.add(PATCH, path, h, m...)
} }
// Post adds a POST route > handler to the router. // Post adds a POST route > handler to the router.
func (e *Echo) Post(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Post(path string, h Handler, m ...Middleware) {
e.add(POST, path, handler, middleware...) e.add(POST, path, h, m...)
} }
// Put adds a PUT route > handler to the router. // Put adds a PUT route > handler to the router.
func (e *Echo) Put(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Put(path string, h Handler, m ...Middleware) {
e.add(PUT, path, handler, middleware...) e.add(PUT, path, h, m...)
} }
// Trace adds a TRACE route > handler to the router. // Trace adds a TRACE route > handler to the router.
func (e *Echo) Trace(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Trace(path string, h Handler, m ...Middleware) {
e.add(TRACE, path, handler, middleware...) e.add(TRACE, path, h, m...)
} }
// Any adds a route > handler to the router for all HTTP methods. // Any adds a route > handler to the router for all HTTP methods.
func (e *Echo) Any(path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Any(path string, handler Handler, middleware ...Middleware) {
for _, m := range methods { for _, m := range methods {
e.add(m, path, handler, middleware...) e.add(m, path, handler, middleware...)
} }
} }
// Match adds a route > handler to the router for multiple HTTP methods provided. // Match adds a route > handler to the router for multiple HTTP methods provided.
func (e *Echo) Match(methods []string, path string, handler interface{}, middleware ...interface{}) { func (e *Echo) Match(methods []string, path string, handler Handler, middleware ...Middleware) {
for _, m := range methods { for _, m := range methods {
e.add(m, path, handler, middleware...) e.add(m, path, handler, middleware...)
} }
} }
// NOTE: v2 func (e *Echo) add(method, path string, handler Handler, middleware ...Middleware) {
func (e *Echo) add(method, path string, handler interface{}, middleware ...interface{}) {
h := wrapHandler(handler)
name := handlerName(handler) name := handlerName(handler)
// middleware = append(e.middleware, middleware...)
// e.router.Add(method, path, handler, e)
e.router.Add(method, path, HandlerFunc(func(c Context) error { e.router.Add(method, path, HandlerFunc(func(c Context) error {
for _, m := range middleware { for _, m := range middleware {
h = wrapMiddleware(m).Handle(h) handler = m.Handle(handler)
} }
return h.Handle(c) return handler.Handle(c)
}), e) }), e)
r := Route{ r := Route{
Method: method, Method: method,
@ -368,14 +374,14 @@ func (e *Echo) add(method, path string, handler interface{}, middleware ...inter
} }
// Group creates a new sub-router with prefix. // Group creates a new sub-router with prefix.
func (e *Echo) Group(prefix string, middleware ...interface{}) (g *Group) { func (e *Echo) Group(prefix string, m ...Middleware) (g *Group) {
g = &Group{prefix: prefix, echo: e} g = &Group{prefix: prefix, echo: e}
g.Use(middleware...) g.Use(m...)
return return
} }
// URI generates a URI from handler. // URI generates a URI from handler.
func (e *Echo) URI(handler interface{}, params ...interface{}) string { func (e *Echo) URI(handler Handler, params ...interface{}) string {
uri := new(bytes.Buffer) uri := new(bytes.Buffer)
ln := len(params) ln := len(params)
n := 0 n := 0
@ -400,8 +406,8 @@ func (e *Echo) URI(handler interface{}, params ...interface{}) string {
} }
// URL is an alias for `URI` function. // URL is an alias for `URI` function.
func (e *Echo) URL(handler interface{}, params ...interface{}) string { func (e *Echo) URL(h Handler, params ...interface{}) string {
return e.URI(handler, params...) return e.URI(h, params...)
} }
// Routes returns the registered routes. // Routes returns the registered routes.
@ -412,15 +418,9 @@ func (e *Echo) Routes() []Route {
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) { func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
c := e.pool.Get().(*context) c := e.pool.Get().(*context)
c.reset(req, res) c.reset(req, res)
h := Handler(c)
// Chain middleware with handler in the end
for i := len(e.middleware) - 1; i >= 0; i-- {
h = e.middleware[i].Handle(h)
}
// Execute chain // Execute chain
if err := h.Handle(c); err != nil { if err := e.head.Handle(c); err != nil {
e.httpErrorHandler(err, c) e.httpErrorHandler(err, c)
} }
@ -473,40 +473,6 @@ func (binder) Bind(r engine.Request, i interface{}) (err error) {
return return
} }
func wrapMiddleware(m interface{}) Middleware { func handlerName(h Handler) string {
switch m := m.(type) { return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
case Middleware:
return m
case MiddlewareFunc:
return m
case func(Handler) Handler:
return MiddlewareFunc(m)
default:
panic("invalid middleware")
}
}
func wrapHandler(h interface{}) Handler {
switch h := h.(type) {
case Handler:
return h
case HandlerFunc:
return h
case func(Context) error:
return HandlerFunc(h)
default:
panic("echo => invalid handler")
}
}
func handlerName(h interface{}) string {
switch h := h.(type) {
case Handler:
t := reflect.TypeOf(h)
return fmt.Sprintf("%s » %s", t.PkgPath(), t.Name())
case HandlerFunc, func(Context) error:
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
default:
panic("echo => invalid handler")
}
} }

View File

@ -44,31 +44,31 @@ func TestEchoMiddleware(t *testing.T) {
e := New() e := New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
e.Use(func(h Handler) Handler { e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
buf.WriteString("a") buf.WriteString("a")
return h.Handle(c) return h.Handle(c)
}) })
}) }))
e.Use(func(h Handler) Handler { e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
buf.WriteString("b") buf.WriteString("b")
return h.Handle(c) return h.Handle(c)
}) })
}) }))
e.Use(func(h Handler) Handler { e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
buf.WriteString("c") buf.WriteString("c")
return h.Handle(c) return h.Handle(c)
}) })
}) }))
// Route // Route
e.Get("/", func(c Context) error { e.Get("/", HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "OK") return c.String(http.StatusOK, "OK")
}) }))
c, b := request(GET, "/", e) c, b := request(GET, "/", e)
assert.Equal(t, "abc", buf.String()) assert.Equal(t, "abc", buf.String())
@ -76,11 +76,11 @@ func TestEchoMiddleware(t *testing.T) {
assert.Equal(t, "OK", b) assert.Equal(t, "OK", b)
// Error // Error
e.Use(func(Handler) Handler { e.Use(MiddlewareFunc(func(Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
return errors.New("error") return errors.New("error")
}) })
}) }))
c, b = request(GET, "/", e) c, b = request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c) assert.Equal(t, http.StatusInternalServerError, c)
} }
@ -89,9 +89,9 @@ func TestEchoHandler(t *testing.T) {
e := New() e := New()
// HandlerFunc // HandlerFunc
e.Get("/ok", func(c Context) error { e.Get("/ok", HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "OK") return c.String(http.StatusOK, "OK")
}) }))
c, b := request(GET, "/ok", e) c, b := request(GET, "/ok", e)
assert.Equal(t, http.StatusOK, c) assert.Equal(t, http.StatusOK, c)
@ -145,23 +145,23 @@ func TestEchoTrace(t *testing.T) {
func TestEchoAny(t *testing.T) { // JFC func TestEchoAny(t *testing.T) { // JFC
e := New() e := New()
e.Any("/", func(c Context) error { e.Any("/", HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "Any") return c.String(http.StatusOK, "Any")
}) }))
} }
func TestEchoMatch(t *testing.T) { // JFC func TestEchoMatch(t *testing.T) { // JFC
e := New() e := New()
e.Match([]string{GET, POST}, "/", func(c Context) error { e.Match([]string{GET, POST}, "/", HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "Match") return c.String(http.StatusOK, "Match")
}) }))
} }
func TestEchoURL(t *testing.T) { func TestEchoURL(t *testing.T) {
e := New() e := New()
static := func(Context) error { return nil } static := HandlerFunc(func(Context) error { return nil })
getUser := func(Context) error { return nil } getUser := HandlerFunc(func(Context) error { return nil })
getFile := func(Context) error { return nil } getFile := HandlerFunc(func(Context) error { return nil })
e.Get("/static/file", static) e.Get("/static/file", static)
e.Get("/users/:id", getUser) e.Get("/users/:id", getUser)
@ -184,9 +184,9 @@ func TestEchoRoutes(t *testing.T) {
{POST, "/repos/:owner/:repo/git/tags", ""}, {POST, "/repos/:owner/:repo/git/tags", ""},
} }
for _, r := range routes { for _, r := range routes {
e.add(r.Method, r.Path, func(c Context) error { e.add(r.Method, r.Path, HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "OK") return c.String(http.StatusOK, "OK")
}) }))
} }
for i, r := range e.Routes() { for i, r := range e.Routes() {
@ -198,15 +198,15 @@ func TestEchoRoutes(t *testing.T) {
func TestEchoGroup(t *testing.T) { func TestEchoGroup(t *testing.T) {
e := New() e := New()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
e.Use(func(h Handler) Handler { e.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
buf.WriteString("0") buf.WriteString("0")
return h.Handle(c) return h.Handle(c)
}) })
}) }))
h := func(c Context) error { h := HandlerFunc(func(c Context) error {
return c.NoContent(http.StatusOK) return c.NoContent(http.StatusOK)
} })
//-------- //--------
// Routes // Routes
@ -216,12 +216,12 @@ func TestEchoGroup(t *testing.T) {
// Group // Group
g1 := e.Group("/group1") g1 := e.Group("/group1")
g1.Use(func(h Handler) Handler { g1.Use(MiddlewareFunc(func(h Handler) Handler {
return HandlerFunc(func(c Context) error { return HandlerFunc(func(c Context) error {
buf.WriteString("1") buf.WriteString("1")
return h.Handle(c) return h.Handle(c)
}) })
}) }))
g1.Get("/", h) g1.Get("/", h)
// Nested groups // Nested groups
@ -251,9 +251,9 @@ func TestEchoNotFound(t *testing.T) {
func TestEchoMethodNotAllowed(t *testing.T) { func TestEchoMethodNotAllowed(t *testing.T) {
e := New() e := New()
e.Get("/", func(c Context) error { e.Get("/", HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, "Echo!") return c.String(http.StatusOK, "Echo!")
}) }))
req := test.NewRequest(POST, "/", nil) req := test.NewRequest(POST, "/", nil)
rec := test.NewResponseRecorder() rec := test.NewResponseRecorder()
e.ServeHTTP(req, rec) e.ServeHTTP(req, rec)
@ -270,9 +270,9 @@ func TestEchoHTTPError(t *testing.T) {
func testMethod(t *testing.T, method, path string, e *Echo) { func testMethod(t *testing.T, method, path string, e *Echo) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:])) m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path) p := reflect.ValueOf(path)
h := reflect.ValueOf(func(c Context) error { h := reflect.ValueOf(HandlerFunc(func(c Context) error {
return c.String(http.StatusOK, method) return c.String(http.StatusOK, method)
}) }))
i := interface{}(e) i := interface{}(e)
reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h}) reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h})
_, body := request(method, path, e) _, body := request(method, path, e)

View File

@ -8,56 +8,53 @@ type (
} }
) )
func (g *Group) Use(middleware ...interface{}) { func (g *Group) Use(m ...Middleware) {
for _, m := range middleware { g.middleware = append(g.middleware, m...)
g.middleware = append(g.middleware, wrapMiddleware(m))
}
} }
func (g *Group) Connect(path string, handler interface{}) { func (g *Group) Connect(path string, h Handler) {
g.add(CONNECT, path, handler) g.add(CONNECT, path, h)
} }
func (g *Group) Delete(path string, handler interface{}) { func (g *Group) Delete(path string, h Handler) {
g.add(DELETE, path, handler) g.add(DELETE, path, h)
} }
func (g *Group) Get(path string, handler interface{}) { func (g *Group) Get(path string, h Handler) {
g.add(GET, path, handler) g.add(GET, path, h)
} }
func (g *Group) Head(path string, handler interface{}) { func (g *Group) Head(path string, h Handler) {
g.add(HEAD, path, handler) g.add(HEAD, path, h)
} }
func (g *Group) Options(path string, handler interface{}) { func (g *Group) Options(path string, h Handler) {
g.add(OPTIONS, path, handler) g.add(OPTIONS, path, h)
} }
func (g *Group) Patch(path string, handler interface{}) { func (g *Group) Patch(path string, h Handler) {
g.add(PATCH, path, handler) g.add(PATCH, path, h)
} }
func (g *Group) Post(path string, handler interface{}) { func (g *Group) Post(path string, h Handler) {
g.add(POST, path, handler) g.add(POST, path, h)
} }
func (g *Group) Put(path string, handler interface{}) { func (g *Group) Put(path string, h Handler) {
g.add(PUT, path, handler) g.add(PUT, path, h)
} }
func (g *Group) Trace(path string, handler interface{}) { func (g *Group) Trace(path string, h Handler) {
g.add(TRACE, path, handler) g.add(TRACE, path, h)
} }
func (g *Group) Group(prefix string, middleware ...interface{}) *Group { func (g *Group) Group(prefix string, m ...Middleware) *Group {
return g.echo.Group(prefix, middleware...) return g.echo.Group(prefix, m...)
} }
func (g *Group) add(method, path string, handler interface{}) { func (g *Group) add(method, path string, h Handler) {
path = g.prefix + path path = g.prefix + path
h := wrapHandler(handler) name := handlerName(h)
name := handlerName(handler)
g.echo.router.Add(method, path, HandlerFunc(func(c Context) error { g.echo.router.Add(method, path, HandlerFunc(func(c Context) error {
for i := len(g.middleware) - 1; i >= 0; i-- { for i := len(g.middleware) - 1; i >= 0; i-- {
h = g.middleware[i].Handle(h) h = g.middleware[i].Handle(h)

View File

@ -4,7 +4,7 @@ import "testing"
func TestGroup(t *testing.T) { func TestGroup(t *testing.T) {
g := New().Group("/group") g := New().Group("/group")
h := func(Context) error { return nil } h := HandlerFunc(func(Context) error { return nil })
g.Connect("/", h) g.Connect("/", h)
g.Delete("/", h) g.Delete("/", h)
g.Get("/", h) g.Get("/", h)

View File

@ -52,7 +52,7 @@ func (r *Router) Handle(h Handler) Handler {
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 h.Handle(c) return c.Handle(c)
}) })
} }