HTTP has a long section about mapping URIs to resources on which you
perform actions. Actions are identified by methods, but aren't used to
find the resource to be used. Cowboy simply follows HTTP.
Adding ways to use methods to map to handlers or functions directly into
Cowboy would just break this concept of resources that Cowboy is trying
hard to protect.
It shouldn't be difficult to write a middleware that allows you to do
this efficiently if that's what you want, or fork the current router to
add the functionality, but that's something that won't land in Cowboy
master.
That's an odd statement, I'm not sure why you would think that's not
what I'm doing. What I'm not doing, however, is make easy things easier
and hard things harder, like Rails does.
> Middleware is a very bad design choice, because it requires duplicating
> code between configuration of router and middleware and it will end as
> throwing away cowboy_router at all.
Router is middleware configuration so that's probably okay.
What I don't really understand is why you need this feature at all
considering you can already have a clause per method easily (which is
fairly similar to a function per method). All you have to do is:
handle(Req, State) ->
handle_method(cowboy_req:get(method, Req), Req, State).
So I'm not sure what you are expecting. If this doesn't work out then
there's plenty other solutions, but I don't know which one is best for
your case.
Then in your init function do something like your_lib:route_method(Req)
which returns {ok, Req, Action} if it's good or {shutdown, ...} if not?
It'll do exactly what you just said.
It builds on the discipline of reference
provided by the Uniform Resource Identifier (URI) [3], as a location
(URL) [4] or name (URN) [20], for indicating the resource to which a
method is to be applied.
It then defines resources as follow on page 9:
A network data object or service that can be identified by a URI,
as defined in section 3.2. Resources may be available in multiple
representations (e.g. multiple languages, data formats, size, and
resolutions) or vary in other ways.
It then describes how to locate resources in many of the following
sections, starting with section 3.2 page 18.
Cowboy implements this. It maps URIs to resources. It makes absolutely
no assumptions as to how your resources operate. Anything that is part
of the URI can and will be used to locate resources. But if it's not
part of the URI, then it's not something used to find resources as
defined by the HTTP standard, which makes it out of the scope of the
project.
It still provides you with many ways to plug yourself into Cowboy to
achieve what you want, which include:
* Middleware
* Protocol upgrade (what REST handlers are doing)
* init/3 based method dispatching
* handle/2 based method dispatching
And I'm sure there's more.
On 02/13/2013 11:42 AM, Max Lapshin wrote:
> Loic, my idea about routing is that router is a piece of software that
> translates input http request into presentation, convenient for handler
> and selects proper handler for this with proper arguments.
>
> I don't understand your logic: you allow to run regexp to validate if
> constraint is a digit: "/users/:user_id", [{user_id,fun() -> .. end}],
> but you are strongly against validating method in constraints.
>
> You tell me to spread logic between your router and my router. You tell
> me that I need to use two routers that will somehow interfere with each
> other. What is the idea? I really don't understand.
>
>
> On Wed, Feb 13, 2013 at 2:39 PM, Loïc Hoguin <es...@ninenines.eu
> <mailto:es...@ninenines.eu>> wrote:
>
> On 02/13/2013 11:34 AM, Max Lapshin wrote:
>
> handle(Req, State) ->
> handle_method(cowboy_req:get(__method, Req), Req, State).