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)
|
||||
e.router.Add(method, path, HandlerFunc(func(c Context) error {
|
||||
h := handler
|
||||
for _, m := range middleware {
|
||||
h = m.Handle(h)
|
||||
// Chain middleware
|
||||
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)
|
||||
}), e)
|
||||
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)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
g = &Group{prefix: prefix, echo: e}
|
||||
g.Use(m...)
|
||||
|
21
echo_test.go
21
echo_test.go
@ -255,17 +255,24 @@ func TestEchoGroup(t *testing.T) {
|
||||
|
||||
// Group
|
||||
g1 := e.Group("/group1")
|
||||
g1.Use(MiddlewareFunc(func(h Handler) Handler {
|
||||
return HandlerFunc(func(c Context) error {
|
||||
g1.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
|
||||
buf.WriteString("1")
|
||||
return h.Handle(c)
|
||||
})
|
||||
}))
|
||||
|
||||
})))
|
||||
g1.Get("", h)
|
||||
|
||||
// Nested groups
|
||||
// Nested groups with middleware
|
||||
g2 := e.Group("/group2")
|
||||
g2.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
|
||||
buf.WriteString("2")
|
||||
return nil
|
||||
})))
|
||||
g3 := g2.Group("/group3")
|
||||
g3.Use(WrapMiddleware(HandlerFunc(func(c Context) error {
|
||||
buf.WriteString("3")
|
||||
return nil
|
||||
})))
|
||||
g3.Get("", h)
|
||||
|
||||
request(GET, "/users", e)
|
||||
@ -276,8 +283,8 @@ func TestEchoGroup(t *testing.T) {
|
||||
assert.Equal(t, "01", buf.String())
|
||||
|
||||
buf.Reset()
|
||||
c, _ := request(GET, "/group2/group3", e)
|
||||
assert.Equal(t, http.StatusOK, c)
|
||||
request(GET, "/group2/group3", e)
|
||||
assert.Equal(t, "023", buf.String())
|
||||
}
|
||||
|
||||
func TestEchoNotFound(t *testing.T) {
|
||||
|
@ -18,20 +18,39 @@ type (
|
||||
|
||||
// Request defines the interface for HTTP request.
|
||||
Request interface {
|
||||
// TLS returns true if connection is TLS otherwise false.
|
||||
// TLS returns true if HTTP connection is TLS otherwise false.
|
||||
TLS() bool
|
||||
|
||||
// Scheme returns the HTTP protocol scheme, `http` or `https`.
|
||||
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
|
||||
|
||||
// URI returns the unmodified `Request-URI` sent by the client.
|
||||
URI() string
|
||||
|
||||
// URL returns `engine.URL`.
|
||||
URL() URL
|
||||
|
||||
// Header returns `engine.Header`.
|
||||
Header() Header
|
||||
|
||||
// Proto() string
|
||||
// ProtoMajor() int
|
||||
// ProtoMinor() int
|
||||
|
||||
// UserAgent returns the client's `User-Agent`.
|
||||
UserAgent() string
|
||||
|
||||
// RemoteAddress returns the client's network address.
|
||||
RemoteAddress() string
|
||||
|
||||
// Method returns the HTTP method of the request.
|
||||
Method() string
|
||||
|
||||
SetMethod(string)
|
||||
Body() io.ReadCloser
|
||||
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 {
|
||||
m = append(g.middleware, 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 {
|
||||
h := handler
|
||||
for _, m := range middleware {
|
||||
h = m.Handle(h)
|
||||
// Chain middleware
|
||||
for i := len(middleware) - 1; i >= 0; i-- {
|
||||
h = middleware[i].Handle(h)
|
||||
}
|
||||
return h.Handle(c)
|
||||
}), g.echo)
|
||||
|
Loading…
Reference in New Issue
Block a user