-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Reverse routing in Cowboy? #816
Comments
There is nothing for that at this point but I suppose it could be added. Sounds interesting. |
A situation where this would be useful is when you're creating a REST API with HATEOAS, eg resources contain links to themselves and related resources. |
Thanks for the swift reply! :)
|
This is interesting, I'll see if I can help. How should it be done?
Another problem is constraint. User-defined constraints can convert bindings from binary to some other type (even pids if you are doing websocket). It would be nice to let binding values be of the converted type too e.g: |
Here are some suggestions:
|
Doing it is one thing, doing it efficiently is another. Perhaps it would make sense to make the router compile to an ets table. But this means we wouldn't be able to update the dispatch dynamically in middlewares (so edge case I doubt anyone does it anyway). With an ets table, we can use match or select functions to get only the routes that might fit, and avoid copying the whole dispatch onto all connection processes. If we have this ets table then we can also use it when we want to do "reverse routing" without having to keep track of the dispatch in Req, saving memory again. The match function would be a little different but the idea is the same: we want all routes that give a handler and then we find the right one from there. Nothing prevents us from making constraint functions accept different types. For example the constraint 'int' would say true for integers and for binaries containing integer values, and false otherwise. Though it's probably not that easy, especially since we want to use constraints in other places too like the new cowboy_req:match functions. Food for thoughts. |
I didn't know you can change route dynamically. I guess it's possible since it's just an environment. But IIRC the cowboy doc already said that one should use I still don't think having one function for both direction of conversion is a good idea. Say if I have a function that convert base64 to raw binary (twitter uuid for example), there wouldn't be an easy way to differentiate. Maybe we can use a pair of function like On fully qualified URL: I did have to return a host name different from the one in the request once. Nothing in HTTP or REST say you can't create a resource somewhere else anyway so I guess it should be optional. ets sounds nice but I'm not sure how one would match a concrete piece of data against a list of patterns. Usually it's the other way around with ets, unless you are talking about just matching against the handler atom. |
You can change the route but that's a hack. The proper way is as you say, so it should be fine. Yes my idea is that we simply match against the handler atom and then have some logic to do the rest. What that logic is remains to be determined, of course. As for constraints, you're right. Perhaps we need a function that receives 2 arguments instead of 1, and we can use the second argument to give context ie say that we are in 'normal' or 'reverse' mode or something. Not sure, this is a tough one. |
A related thing of interest for HATEOAS is parsing URLs using the router rules without doing actual routing. Exporting |
What's the use case for that? What do you do with the info? |
The user POSTs to |
Yep, sounds good. Will be considered. |
I think the functionality of reverse routing would be too limited, if implemented. The problem is that we can't go from "handler module + bindings" to getting the host (unless it's used exactly once), that wildcards would cause problems, that reusing handlers for more than one path would not be resolvable (for example if you have generic handlers and reuse them for many different types of resources). I do not think it wise to implement something that leaves so much uncertainty in its behavior, and only works well if routes are defined a certain way. On the other hand exposing cowboy_router:match is still a consideration and will happen, perhaps not in 2.0 but soon after. |
Ok, thanks for the update! Looking forward to 2.0 ! 🙂
…On Sat, 10 Jun 2017 at 13:05, Loïc Hoguin ***@***.***> wrote:
I think the functionality of reverse routing would be too limited, if
implemented. The problem is that we can't go from "handler module +
bindings" to getting the host (unless it's used exactly once), that
wildcards would cause problems, that reusing handlers for more than one
path would not be resolvable (for example if you have generic handlers and
reuse them for many different types of resources). I do not think it wise
to implement something that leaves so much uncertainty in its behavior, and
only works well if routes are defined a certain way.
On the other hand exposing cowboy_router:match is still a consideration
and will happen, perhaps not in 2.0 but soon after.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#816 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABMY9vdBny5D5XAIkFOKWGuSMvvMypv1ks5sCniEgaJpZM4EPcxH>
.
|
I've done both reverse constraints in c221730. Feedback welcome! |
I've implemented URI templates in Cowlib https://github.com/ninenines/cowlib/blob/master/src/cow_uri_template.erl It is one step in the reverse routing direction. I am considering moving the router to use the URI template syntax + constraints, either in 3.0 or optionally for now. Then the template can always be expanded, perhaps via a Anyway it's still a long way off. |
Is it possible to do "reverse routing" in Cowboy, i.e. to generate a URL given a handler + bindings?
For example, given a handler (e.g.
'user_handler'
) and a set of URL bindings (e.g.[{user_id, 1234}]
) is it possible to generate a URL that will resolve to the given handler + bindings? (e.g."/users/1234“
)The text was updated successfully, but these errors were encountered: