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

Already on GitHub? Sign in to your account

Implement explicit API for creating a router and adding routes. #1

Open
wants to merge 4 commits into
from

Conversation

Projects
None yet
2 participants

fcuny commented Apr 6, 2013

Add a NewRouter function to the router package to explicitly create a
new router. The functions AddRoute and AddRoutes are added in order
to explicitly add routes to the router. The AddRoute function also
check that the route is unique during that phase, and report an error if
it's not the case.

The tests and the example are updated to reflect that change.

Implement explicit API for creating a router and adding routes.
Add a `NewRouter` function to the router package to explicitly create a
new router.  The functions `AddRoute` and `AddRoutes` are added in order
to explicitly add routes to the router.  The `AddRoute` function also
check that the route is unique during that phase, and report an error if
it's not the case.

The tests and the example are updated to reflect that change.
Owner

ant0ine commented Apr 7, 2013

This is interesting, I noticed that most of the std lib packages try to not have constructor method. Instead they want to user to instantiate the object using the literal way. For this reason I didn't created constructor for this object.
That been said, having a constructor now, allows for additional logic in the future without changing the API.
So really, I'm 50/50 on this question. Is it a good practice to define constructor in Go or is it a good practice to avoid them ? I need to investigate that.

fcuny commented Apr 7, 2013

As you said, having a constructor allows additional logic when creating the router. The way I see it, the router is not an ordinary object/type, but some kind of higher level object, and having a constructor to deal with (future) logic make sense (at least to me).

I would agree that it would make less sense for something like the Route.

And what about the AddRoute(s) functions? If the constructor is an issue I can rework the patch to remove it.

fcuny added some commits Apr 7, 2013

Separate the HTTP method from the path in the route.
A route is the addition of a path and an HTTP method.  The HTTP method
is stored along the path for each route.

By default, if a route is created without the HTTP method specified,
routes for the GET PUT POST DELETE methods are created.

This should allow some introspection on route in the future.
Maintain a list of routes and their methods.
This is useful for future introspection features.
Add default OPTIONS route.
Unless this feature is disabled, a default OPTIONS route is added for
every exiting route.  Also, add a 405 Method Not Available for all the
non existing methods for a given route.

This way, if you define a GET and DELETE for a route, but no POST, PUT
and PATCH, a 405 will be returned to the client if it tries to reach one
of this endpoint.

fcuny commented Apr 8, 2013

This is not final, I'm still improving and simplifying the code added to support this feature.

Owner

ant0ine commented Apr 8, 2013

Wow, that's a lot of work :)
Last week I've changed the implementation of go-json-rest to use one router per method:
ant0ine/go-json-rest@b3ca9f1

The idea is that on a REST framework you have to know the http method by definition. While on Go-UrlRouter that may be used for a different purpose, you may ask "find the route for this Path and ANY Method". Which mean that there is a special kind of placeholder to implement in the Trie. I decided to keep the things simple by not supporting the http Method in Go-UrlRouter. Lets discuss the Pros and Cons next week :)
Thanks for all the work !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment