mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Fixed nested group middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
9795e59c43
commit
3abe661e72
10
echo.go
10
echo.go
@ -391,9 +391,13 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
|
|||||||
name := handlerName(handler)
|
name := handlerName(handler)
|
||||||
e.router.Add(method, path, HandlerFunc(func(c Context) error {
|
e.router.Add(method, path, HandlerFunc(func(c Context) error {
|
||||||
h := handler
|
h := handler
|
||||||
for _, m := range middleware {
|
// Chain middleware
|
||||||
h = m.Handle(h)
|
for i := len(middleware) - 1; i >= 0; i-- {
|
||||||
|
h = middleware[i].Handle(h)
|
||||||
}
|
}
|
||||||
|
// for _, m := range middleware {
|
||||||
|
// h = m.Handle(h)
|
||||||
|
// }
|
||||||
return h.Handle(c)
|
return h.Handle(c)
|
||||||
}), e)
|
}), e)
|
||||||
r := Route{
|
r := Route{
|
||||||
@ -404,7 +408,7 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
|
|||||||
e.router.routes = append(e.router.routes, r)
|
e.router.routes = append(e.router.routes, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group creates a new sub-router with prefix.
|
// Group creates a new router group with prefix.
|
||||||
func (e *Echo) Group(prefix string, m ...Middleware) (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(m...)
|
g.Use(m...)
|
||||||
|
21
echo_test.go
21
echo_test.go
@ -255,17 +255,24 @@ func TestEchoGroup(t *testing.T) {
|
|||||||
|
|
||||||
// Group
|
// Group
|
||||||
g1 := e.Group("/group1")
|
g1 := e.Group("/group1")
|
||||||
g1.Use(MiddlewareFunc(func(h Handler) Handler {
|
g1.Use(WrapMiddleware(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 with middleware
|
||||||
g2 := e.Group("/group2")
|
g2 := e.Group("/group2")
|
||||||
|
g2.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
|
||||||
|
buf.WriteString("2")
|
||||||
|
return nil
|
||||||
|
})))
|
||||||
g3 := g2.Group("/group3")
|
g3 := g2.Group("/group3")
|
||||||
|
g3.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
|
||||||
|
buf.WriteString("3")
|
||||||
|
return nil
|
||||||
|
})))
|
||||||
g3.Get("", h)
|
g3.Get("", h)
|
||||||
|
|
||||||
request(GET, "/users", e)
|
request(GET, "/users", e)
|
||||||
@ -276,8 +283,8 @@ func TestEchoGroup(t *testing.T) {
|
|||||||
assert.Equal(t, "01", buf.String())
|
assert.Equal(t, "01", buf.String())
|
||||||
|
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
c, _ := request(GET, "/group2/group3", e)
|
request(GET, "/group2/group3", e)
|
||||||
assert.Equal(t, http.StatusOK, c)
|
assert.Equal(t, "023", buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoNotFound(t *testing.T) {
|
func TestEchoNotFound(t *testing.T) {
|
||||||
|
@ -18,20 +18,39 @@ type (
|
|||||||
|
|
||||||
// Request defines the interface for HTTP request.
|
// Request defines the interface for HTTP request.
|
||||||
Request interface {
|
Request interface {
|
||||||
// TLS returns true if connection is TLS otherwise false.
|
// TLS returns true if HTTP connection is TLS otherwise false.
|
||||||
TLS() bool
|
TLS() bool
|
||||||
|
|
||||||
|
// Scheme returns the HTTP protocol scheme, `http` or `https`.
|
||||||
Scheme() string
|
Scheme() string
|
||||||
|
|
||||||
|
// Host returns HTTP request host. // For server requests Host specifies the host on which the
|
||||||
|
// URL is sought. Per RFC 2616, this is either the value of
|
||||||
|
// the "Host" header or the host name given in the URL itself.
|
||||||
Host() string
|
Host() string
|
||||||
|
|
||||||
|
// URI returns the unmodified `Request-URI` sent by the client.
|
||||||
URI() string
|
URI() string
|
||||||
|
|
||||||
|
// URL returns `engine.URL`.
|
||||||
URL() URL
|
URL() URL
|
||||||
|
|
||||||
|
// Header returns `engine.Header`.
|
||||||
Header() Header
|
Header() Header
|
||||||
|
|
||||||
// Proto() string
|
// Proto() string
|
||||||
// ProtoMajor() int
|
// ProtoMajor() int
|
||||||
// ProtoMinor() int
|
// ProtoMinor() int
|
||||||
|
|
||||||
|
// UserAgent returns the client's `User-Agent`.
|
||||||
UserAgent() string
|
UserAgent() string
|
||||||
|
|
||||||
|
// RemoteAddress returns the client's network address.
|
||||||
RemoteAddress() string
|
RemoteAddress() string
|
||||||
|
|
||||||
|
// Method returns the HTTP method of the request.
|
||||||
Method() string
|
Method() string
|
||||||
|
|
||||||
SetMethod(string)
|
SetMethod(string)
|
||||||
Body() io.ReadCloser
|
Body() io.ReadCloser
|
||||||
FormValue(string) string
|
FormValue(string) string
|
||||||
|
7
group.go
7
group.go
@ -60,7 +60,9 @@ func (g *Group) Match(methods []string, path string, handler Handler, middleware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Group creates a new sub-group with prefix.
|
||||||
func (g *Group) Group(prefix string, m ...Middleware) *Group {
|
func (g *Group) Group(prefix string, m ...Middleware) *Group {
|
||||||
|
m = append(g.middleware, m...)
|
||||||
return g.echo.Group(g.prefix+prefix, m...)
|
return g.echo.Group(g.prefix+prefix, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,8 +73,9 @@ func (g *Group) add(method, path string, handler Handler, middleware ...Middlewa
|
|||||||
|
|
||||||
g.echo.router.Add(method, path, HandlerFunc(func(c Context) error {
|
g.echo.router.Add(method, path, HandlerFunc(func(c Context) error {
|
||||||
h := handler
|
h := handler
|
||||||
for _, m := range middleware {
|
// Chain middleware
|
||||||
h = m.Handle(h)
|
for i := len(middleware) - 1; i >= 0; i-- {
|
||||||
|
h = middleware[i].Handle(h)
|
||||||
}
|
}
|
||||||
return h.Handle(c)
|
return h.Handle(c)
|
||||||
}), g.echo)
|
}), g.echo)
|
||||||
|
Loading…
Reference in New Issue
Block a user