9 // Group is a set of sub-routes for a specified route. It can be used for inner
10 // routes that share a common middleware or functionality that should be separate
11 // from the parent echo instance while still inheriting from it.
14 middleware []MiddlewareFunc
19 // Use implements `Echo#Use()` for sub-routes within the Group.
20 func (g *Group) Use(middleware ...MiddlewareFunc) {
21 g.middleware = append(g.middleware, middleware...)
22 // Allow all requests to reach the group as they might get dropped if router
23 // doesn't find a match, making none of the group middleware process.
24 for _, p := range []string{"", "/*"} {
25 g.echo.Any(path.Clean(g.prefix+p), func(c Context) error {
26 return NotFoundHandler(c)
31 // CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
32 func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
33 return g.Add(http.MethodConnect, path, h, m...)
36 // DELETE implements `Echo#DELETE()` for sub-routes within the Group.
37 func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
38 return g.Add(http.MethodDelete, path, h, m...)
41 // GET implements `Echo#GET()` for sub-routes within the Group.
42 func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
43 return g.Add(http.MethodGet, path, h, m...)
46 // HEAD implements `Echo#HEAD()` for sub-routes within the Group.
47 func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
48 return g.Add(http.MethodHead, path, h, m...)
51 // OPTIONS implements `Echo#OPTIONS()` for sub-routes within the Group.
52 func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
53 return g.Add(http.MethodOptions, path, h, m...)
56 // PATCH implements `Echo#PATCH()` for sub-routes within the Group.
57 func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
58 return g.Add(http.MethodPatch, path, h, m...)
61 // POST implements `Echo#POST()` for sub-routes within the Group.
62 func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
63 return g.Add(http.MethodPost, path, h, m...)
66 // PUT implements `Echo#PUT()` for sub-routes within the Group.
67 func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
68 return g.Add(http.MethodPut, path, h, m...)
71 // TRACE implements `Echo#TRACE()` for sub-routes within the Group.
72 func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
73 return g.Add(http.MethodTrace, path, h, m...)
76 // Any implements `Echo#Any()` for sub-routes within the Group.
77 func (g *Group) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
78 routes := make([]*Route, len(methods))
79 for i, m := range methods {
80 routes[i] = g.Add(m, path, handler, middleware...)
85 // Match implements `Echo#Match()` for sub-routes within the Group.
86 func (g *Group) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
87 routes := make([]*Route, len(methods))
88 for i, m := range methods {
89 routes[i] = g.Add(m, path, handler, middleware...)
94 // Group creates a new sub-group with prefix and optional sub-group-level middleware.
95 func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
96 m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
97 m = append(m, g.middleware...)
98 m = append(m, middleware...)
99 return g.echo.Group(g.prefix+prefix, m...)
102 // Static implements `Echo#Static()` for sub-routes within the Group.
103 func (g *Group) Static(prefix, root string) {
104 static(g, prefix, root)
107 // File implements `Echo#File()` for sub-routes within the Group.
108 func (g *Group) File(path, file string) {
109 g.echo.File(g.prefix+path, file)
112 // Add implements `Echo#Add()` for sub-routes within the Group.
113 func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
114 // Combine into a new slice to avoid accidentally passing the same slice for
115 // multiple routes, which would lead to later add() calls overwriting the
116 // middleware from earlier calls.
117 m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
118 m = append(m, g.middleware...)
119 m = append(m, middleware...)
120 return g.echo.Add(method, g.prefix+path, handler, m...)