-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Routes and onhandlers #2337
Routes and onhandlers #2337
Conversation
…thod+path in list of routes.
…ed when new route is registered.
Codecov ReportBase: 92.37% // Head: 92.75% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #2337 +/- ##
==========================================
+ Coverage 92.37% 92.75% +0.37%
==========================================
Files 37 37
Lines 4446 4471 +25
==========================================
+ Hits 4107 4147 +40
+ Misses 247 236 -11
+ Partials 92 88 -4
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
@lammel could you review. I think this would be last PR for next release. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Only some minor questions arised and some suggestions for comments.
But nothing to hold me back from saying.... Approved!
} | ||
|
||
// Add does not add route. because of backwards compatibility we can not change this method signature | ||
route.Add("LOCK", "/users", handlerFunc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you detail why Add()
does not add a route?
If it is just a test for backward compatibility it should not be in the testcase loop here I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because Add
signature is func (r *Router) Add(method, path string, h HandlerFunc) {
and add
signature is func (r *Router) add(method, path, name string, h HandlerFunc) *Route {
and I needed way to return routes from Router.
add
was introduced so we could create Route
object inside router. As router should be the creator of routes not Echo object. Previously it was done inside echo.add
method and cause problems when multiple different host
s are used in application
Lines 530 to 545 in 8d4ac4c
func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route { | |
name := handlerName(handler) | |
router := e.findRouter(host) | |
// FIXME: when handler+middleware are both nil ... make it behave like handler removal | |
router.Add(method, path, func(c Context) error { | |
h := applyMiddleware(handler, middleware...) | |
return h(c) | |
}) | |
r := &Route{ | |
Method: method, | |
Path: path, | |
Name: name, | |
} | |
e.router.routes[method+path] = r | |
return r | |
} |
and this Add
call in this test is to document that behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to summarize: This test ensures the routes added via Add() do not show up in non-global routers.
Maybe you can improve the comment. I had to lookup the code
Suggestion (please verify if it makes sense):
- // Add does not add route. because of backwards compatibility we can not change this method signature
+ // Verify backwards compatibility for documented behavior for router.Add()
+ // Routes added via Add() shall not show up for dedicated routers (e.g. used for host based routes)
route.Add("LOCK", "/users", handlerFunc)
@lammel please re-review. I changed the suggested comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fix situation when Echo instance is used to serve multiple hosts. In this case all registered routes are seen from
e.Routes()
map but problem arises when multiple hosts have routes with same method+path - in this case latest added will only be ine.Routes()
output.e.Routes()
will only report routes added to default router (hosts = "")e.Routers()["domain2.router.com"].Routes()
Reverse(name string, params ...interface{}) string
. Echos ownReverse()
will call default routerReverse
now.Added handler to echo instance to help keeping track what routes are registered in a centralized way. There is new handler field: