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

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

merged 1 commit into from Jul 4, 2017

Conversation

nicklaw5
Copy link
Contributor

@nicklaw5 nicklaw5 commented Jun 30, 2017

This exposes the echo.add (becomes echo.Add) method which makes it easy to iterate over a map of routes and dynamically register them.

For example:

package main

import (
	"log"
	"net/http"

	"github.com/labstack/echo"
)

type Route struct {
	Path    string
	Method  string
	Handler echo.HandlerFunc
}

var routes = []*Route{
	&Route{
		Path:   "/api/users",
		Method: echo.GET,
		Handler: func(c echo.Context) error {
			return c.String(http.StatusOK, "Listing users...")
		},
	},
	&Route{
		Path:   "/api/users",
		Method: echo.POST,
		Handler: func(c echo.Context) error {
			return c.String(http.StatusOK, "Creating user...")
		},
	},
}

func main() {
	e := echo.New()

	for _, route := range routes {
		e.Add(route.Method, route.Path, route.Handler)
	}

	if err := e.Start(":8888"); err != nil {
		log.Fatalf("Failed to start server: %s", err.Error())
	}
}

@codecov
Copy link

codecov bot commented Jun 30, 2017

Codecov Report

Merging #965 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master    #965   +/-   ##
======================================
  Coverage    78.2%   78.2%           
======================================
  Files          27      27           
  Lines        1831    1831           
======================================
  Hits         1432    1432           
  Misses        278     278           
  Partials      121     121
Impacted Files Coverage Δ
echo.go 87.1% <100%> (ø) ⬆️
group.go 93.02% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ee85b46...cedbe9a. Read the comment docs.

echo.go Outdated
@@ -460,7 +460,7 @@ func (e *Echo) File(path, file string) *Route {
})
}

func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
Copy link
Member

Choose a reason for hiding this comment

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

Can you put a godoc like Any or similar methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

godoc added.

echo.go Outdated
@@ -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 all HTTP methods and path with matching handler
Copy link
Member

Choose a reason for hiding this comment

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

Not for all methods, it should be "for an HTTP method and path ..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my head's not int the game today 😞

Should it be "for an HTTP method..." or "for a HTTP method..."?

Copy link
Member

Choose a reason for hiding this comment

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

Lets stick to an.

@vishr vishr mentioned this pull request Jul 4, 2017
@vishr vishr merged commit e0ea129 into labstack:master Jul 4, 2017
@vishr
Copy link
Member

vishr commented Jul 4, 2017

@nicklaw5 thanks for your contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants