-
Notifications
You must be signed in to change notification settings - Fork 266
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
Is there a cleaner way to compose with routef? #223
Comments
Hi, Yes, you could also do this: let webApp =
mustBeAdmin >=> choose [
routef "/....." handler1
routef "/....." handler2
] |
In my app I want to have some routes secured and some not, just like in the sample app, so I want to apply mustBeAdmin on per-route basis. And this works fine with route, but not fine with routef |
Ok I understand... can you give me a more concrete example? The
See https://github.com/giraffe-fsharp/Giraffe/blob/master/DOCUMENTATION.md#subroute for more info. If this is not what you need, could you please provide me a simple example of what you would want to achieve? Thank you |
This is another example: let webApp : HttpHandler =
choose [
// Not secured
route "/" >=> text "Hello"
route "/foo" >=> text "Bar"
// Secured without subRoute
mustBeLoggedIn >=>
choose [
route "/user/" >=> text "Hello"
route "/user/foo" >=> text "Bar"
]
// Secured with subRoute
subRoute "/secure" (
mustBeLoggedIn >=>
choose [
routef "/%s" handler1
routef "/%s/%i" handler2
]
)
] Essentially you can chain and compose http handlers however you wish! |
So, here is the sample, I want this urls:
Is there a way to implement this? |
I see, looks like you mean that I need to exchange authorize and routef? And all private rules should go under the public ones, right? |
I think that there is one issue there. If user enteres the wrong url, it will go through the authorization step first, which includes database interaction, while it could be just rejected by the wrong url. I think urls check should go before the user verification. |
Right.. because you have a dynamic To be fair this is the first use case where I see how a |
Ok, thank you for the response |
I have added a See subRoutef |
Thank you! But looks like there will still be no way to silently pass the lang argument further in the compose pipe. I'm just comparing it with asp.net mvc were you just put [Authorize] attribute regardless of the route, but the workaround in your example looks sufficient, at least for now I don't think the surrounding function can become too complex. |
Given your routes...
You can do this now: let webApp : HttpHandler =
subRoutef "/%s" (fun lang ->
choose [
subRoute "/articles" (
choose [
route "/top" >=> getTopArticles lang
routef "/%s" (getArticle lang)
])
route "/dashboard" >=> authorize >=> getDashboard lang
]
) or: let webApp : HttpHandler =
subRoutef "/%s" (
fun lang ->
choose [
route "/articles/top" >=> getTopArticles lang
routef "/articles/%s" (getArticle lang)
route "/dashboard" >=> authorize >=> getDashboard lang
]
) or: let webApp : HttpHandler =
choose [
routef "/%s/articles/top" getTopArticles
routef "/%s/articles/%s" getArticle
subRoutef "/%s" (
fun lang ->
route "/dashboard"
>=> authorize
>=> getDashboard lang
)
] The I thought this was ultimately what you wanted right? |
But, if you don't want to pre-parse the first parameter, there is another option which will work as well. For actually a very different reason (trailing slash handling) I have just finished implementing a With that you could check the route is correct before doing authorize and then use the full let webApp : HttpHandler =
choose [
routef "/%s/articles/top" getTopArticles
routef "/%s/articles/%s" getArticle
routex "/[a-z]{2}/dashboard" >=> authorize >=> routef "/%s/dashboard" getDashboard
] What do you think? |
You are right, I wanted to be less verbose and don't specify lang parameter in the route configuration code. For example I could also have private route /en/my-articles/123 with route /%s/my-articles/%s, so this will introduce more nested functions. The solution with routex looks more attractive to me, however I'm a bit worried about performance if regex is used there =) |
Fair enough :). The regex is compiled so should be considerably faster than normal, but I understand your concerns. Unfortunately this is the best I can offer for now. |
I see, thank you for such quick cooperation anyway! |
In the sample file there are lines
https://github.com/giraffe-fsharp/Giraffe/blob/master/samples/SampleApp/SampleApp/Program.fs#L71-L73
So mustBeAdmin filter moves from all routes configuration to particular handler. Is there a better way to have both routef and mustBeAdmin composed together like route routes?
The text was updated successfully, but these errors were encountered: