Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose echo.add() method for dynamic route registration #965

Merged
merged 1 commit into from Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions echo.go
Expand Up @@ -356,63 +356,63 @@ func (e *Echo) Use(middleware ...MiddlewareFunc) {
// CONNECT registers a new CONNECT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(CONNECT, path, h, m...)
return e.Add(CONNECT, path, h, m...)
}

// DELETE registers a new DELETE route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(DELETE, path, h, m...)
return e.Add(DELETE, path, h, m...)
}

// GET registers a new GET route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(GET, path, h, m...)
return e.Add(GET, path, h, m...)
}

// HEAD registers a new HEAD route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(HEAD, path, h, m...)
return e.Add(HEAD, path, h, m...)
}

// OPTIONS registers a new OPTIONS route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(OPTIONS, path, h, m...)
return e.Add(OPTIONS, path, h, m...)
}

// PATCH registers a new PATCH route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(PATCH, path, h, m...)
return e.Add(PATCH, path, h, m...)
}

// POST registers a new POST route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(POST, path, h, m...)
return e.Add(POST, path, h, m...)
}

// PUT registers a new PUT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(PUT, path, h, m...)
return e.Add(PUT, path, h, m...)
}

// TRACE registers a new TRACE route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.add(TRACE, path, h, m...)
return e.Add(TRACE, path, h, m...)
}

// Any registers a new route for all HTTP methods and path with matching handler
// in the router with optional route-level middleware.
func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
routes := make([]*Route, 0)
for _, m := range methods {
routes = append(routes, e.add(m, path, handler, middleware...))
routes = append(routes, e.Add(m, path, handler, middleware...))
}
return routes
}
Expand All @@ -422,7 +422,7 @@ func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFun
func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
routes := make([]*Route, 0)
for _, m := range methods {
routes = append(routes, e.add(m, path, handler, middleware...))
routes = append(routes, e.Add(m, path, handler, middleware...))
}
return routes
}
Expand Down Expand Up @@ -460,7 +460,9 @@ func (e *Echo) File(path, file string) *Route {
})
}

func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
// Add registers a new route for an HTTP method and path with matching handler
// in the router with optional route-level middleware.
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
name := handlerName(handler)
e.router.Add(method, path, func(c Context) error {
h := handler
Expand Down
2 changes: 1 addition & 1 deletion echo_test.go
Expand Up @@ -285,7 +285,7 @@ func TestEchoRoutes(t *testing.T) {
{POST, "/repos/:owner/:repo/git/tags", ""},
}
for _, r := range routes {
e.add(r.Method, r.Path, func(c Context) error {
e.Add(r.Method, r.Path, func(c Context) error {
return c.String(http.StatusOK, "OK")
})
}
Expand Down
2 changes: 1 addition & 1 deletion group.go
Expand Up @@ -109,5 +109,5 @@ func (g *Group) add(method, path string, handler HandlerFunc, middleware ...Midd
m := []MiddlewareFunc{}
m = append(m, g.middleware...)
m = append(m, middleware...)
return g.echo.add(method, g.prefix+path, handler, m...)
return g.echo.Add(method, g.prefix+path, handler, m...)
}