-
Notifications
You must be signed in to change notification settings - Fork 68
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
Router scope #150
Router scope #150
Conversation
The name Also, why do we need to combine adding multiple middleware to prefixing routes? Can't these be separate functions? |
@rgrinberg A common use case for this function is an admin interface. You'd want to prefix all the routes with something like This could be split into two functions, but I think the use case is common enough that it makes sense to have a function that does both at the same time. The parameters are optional, so one can only prefix routes by not passing a The name "scope" is borrowed from Elixir's Phoenix, where a typical route looks like this:
If you'd still want to split them into two functions, maybe the API could look like this:
But TBH I find this API a bit confusing. |
@rgrinberg Here are some alternative APIs for this, let me know if one of them makes sense to you 🙂 Separated functionslet router =
let open Router in
[ get "" Handler.dummy
; get "/:id/settings" Handler.dummy
; get "/new" Handler.dummy
; get "/:id" Handler.dummy
]
|> with_prefix "/projects"
|> with_middlewares Middleware.[ require_authenticated_user; fetch_current_user ]
|> add_routes
;; Arguments to
|
I'll let you make the call. Tbh, I'm not sure what's a user friendly naming convention here. Let me just make a couple of general points:
|
Closing this now, we can continue the discussion in #215 |
This PR adds a
Router.scope
function to easily build routers where different middlewares are applied to the routes.This allows to add routes to the router with the following syntax: