2015-05-23 05:26:52 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
type (
|
2016-03-20 00:47:20 +02:00
|
|
|
// Group is a set of sub-routes for a specified route. It can be used for inner
|
|
|
|
// routes that share a common middlware or functionality that should be separate
|
|
|
|
// from the parent echo instance while still inheriting from it.
|
2015-05-23 05:26:52 +02:00
|
|
|
Group struct {
|
2016-02-15 18:11:29 +02:00
|
|
|
prefix string
|
2016-04-02 23:19:39 +02:00
|
|
|
middleware []MiddlewareFunc
|
2016-02-15 18:11:29 +02:00
|
|
|
echo *Echo
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
)
|
2015-05-23 06:24:35 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Use implements `Echo#Use()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Use(m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
g.middleware = append(g.middleware, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Connect implements `Echo#Connect()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(CONNECT, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Delete implements `Echo#Delete()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(DELETE, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Get implements `Echo#Get()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(GET, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Head implements `Echo#Head()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(HEAD, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Options implements `Echo#Options()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(OPTIONS, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Patch implements `Echo#Patch()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(PATCH, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Post implements `Echo#Post()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(POST, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Put implements `Echo#Put()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(PUT, path, h, m...)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Trace implements `Echo#Trace()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
g.add(TRACE, path, h, m...)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Any implements `Echo#Any()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
for _, m := range methods {
|
|
|
|
g.add(m, path, handler, middleware...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Match implements `Echo#Match()` for sub-routes within the Group.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2016-02-20 18:11:02 +02:00
|
|
|
for _, m := range methods {
|
|
|
|
g.add(m, path, handler, middleware...)
|
|
|
|
}
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Group creates a new sub-group with prefix and optional sub-group-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) Group(prefix string, m ...MiddlewareFunc) *Group {
|
2016-03-17 05:15:38 +02:00
|
|
|
m = append(g.middleware, m...)
|
2016-03-09 05:31:11 +02:00
|
|
|
return g.echo.Group(g.prefix+prefix, m...)
|
2015-05-27 23:07:52 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func (g *Group) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2016-02-15 18:11:29 +02:00
|
|
|
path = g.prefix + path
|
2016-02-20 18:11:02 +02:00
|
|
|
name := handlerName(handler)
|
|
|
|
middleware = append(g.middleware, middleware...)
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
g.echo.router.Add(method, path, func(c Context) error {
|
2016-03-15 17:33:57 +02:00
|
|
|
h := handler
|
2016-03-17 05:15:38 +02:00
|
|
|
// Chain middleware
|
|
|
|
for i := len(middleware) - 1; i >= 0; i-- {
|
2016-04-02 23:19:39 +02:00
|
|
|
h = middleware[i](h)
|
2016-02-15 18:11:29 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return h(c)
|
|
|
|
}, g.echo)
|
2016-02-15 18:11:29 +02:00
|
|
|
r := Route{
|
|
|
|
Method: method,
|
|
|
|
Path: path,
|
|
|
|
Handler: name,
|
|
|
|
}
|
|
|
|
g.echo.router.routes = append(g.echo.router.routes, r)
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|