-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add route_prefix
macro for prefixing/namespacing routes
#1087
Comments
That would be perfect! Will param interpolation work there as well? For example: abstract class ShopAction < Lucky::Action
route_prefix "/shops/:handle"
end |
@wout yes! It’ll basically copy paste the prefix as is. So anything you’d do in get/post/etc would work in route prefix |
I'd like to work on this, should this macro go in the routable module? |
@rnice01 Yeah, I think that's what I'd probably put it. That module is included in all of the actions already. |
@rnice01 Thanks for contributing! This will almost certainly require some macros. They can be a bit tricky to work with so if you need any help feel free to ping me |
Hey @paulcsmith , I'm a bit stuck on this one 😅 . So I'm not sure the best way to accomplish this but here's what I have so far. I'm trying to define a method that will return the prefix if the ` # Defines the prefix for all |
Hey @rnice01! Yeah this is a tricky one for sure! Because paths are parsed at compile time, the macro likely needs to use a constant so that it is accessible at compile time. I have not test this but I imagine it to look something like this: module Routable
macro included
ROUTE_SETTINGS = {prefix: nil}
end
macro route_prefix(prefix)
# Should also check that it is a `StringLiteral` and raise if it isn't
# It should also start with a slash. See how to do that here: https://github.com/luckyframework/lucky/blob/cb207039388c2289fcb9408b92ce18a9c9825d4c/src/lucky/routable.cr#L47-L49
{% ROUTE_SETTINGS[:prefix] = prefix %}
end
macro add_route(method, path, action)
Lucky::Router.add({{ method }}, {{ ROUTE_SETTINGS[:prefix] }}{{ path }}, {{ @type.name.id }})
# The other stuff
end
end Does that help? |
@paulcsmith Yes that was very helpful, thank you! I did however notice that other actions were getting prefixed as well. It appears as though the problem comes from the constant not being overridden by subclasses. This behavior appears to be intentional as noted in this issue. I tried using the syntax suggested in the thread and was having some difficulty getting it to compile. I'll work on it some more this weekend to see if I can get it working properly. |
@rnice01 Oh that is a great point If you look in other spots of the codebase you can see how we "inherited" constants by coping them. Lines 2 to 17 in 522b207
Maybe that helps! And feel free to post back here or in Gitter if you wanna chat. This stuff is triiicky! |
#834 (comment)
Would prefix routes that inherit from this class with
/api/v1
so you don't have to do it for every single routeThe text was updated successfully, but these errors were encountered: