Skip to content

Commit

Permalink
Expose echo.Add() for dynamic route registration (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklaw5 authored and vishr committed Jul 4, 2017
1 parent ee85b46 commit e0ea129
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
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...)
}

3 comments on commit e0ea129

@paulwalker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any documentation on this?

@paulwalker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fairly straightforward obviously...it eliminated the ugly use of a switch statement in my code. This should get documented in the routing section of the guide though.

@vishr
Copy link
Member

@vishr vishr commented on e0ea129 Jul 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paultyng Glad you figured it out. I would appreciate if you can contribute to the document here https://github.com/labstack/echo/blob/master/website/content/guide/routing.md

Please sign in to comment.