Skip to content
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

Using middleware without a scope prefix? #371

Open
yawaramin opened this issue Dec 22, 2024 · 1 comment
Open

Using middleware without a scope prefix? #371

yawaramin opened this issue Dec 22, 2024 · 1 comment

Comments

@yawaramin
Copy link
Contributor

yawaramin commented Dec 22, 2024

Can we add a function Dream.use : middleware list -> route list -> route to add middlewares to a bunch of routes without needing a scope prefix? My use case is that in bidirectional routing, we typically create route or path 'objects' which need to be able to print out the correct path for linking purposes:

let order = [%path "/orders/%s"]

let get_order = get order (fun request order_id ->
  ...
  a [href (Path.link order) order_id] [txt "Your order"]
  ...
)

If we attach a middleware to this route with eg Dream.scope "/v2" [v2] [get_order], the path object no longer captures the correct routable path, which will be /v2/orders/:param3. But if we capture the full path:

let order = [%path "/v2/orders/%s"]

Then we need to attach middlewares without a prefix:

use [v2] [
  get_order;
  post_order;
  delete_order;
]

I have a simple working implementation now:

let use middlewares = Dream.scope "" middlewares

But something tells me this may not be the best approach. Could we support it directly in Dream?

EDIT: prior art for the name use:

EDIT 2: the OCaml website people are also doing Dream.scope "": https://github.com/ocaml/ocaml.org/blob/5ac9eea5813c8813d4a562958af7a99d910bba81/src/ocamlorg_web/lib/router.ml#L79

yawaramin added a commit to yawaramin/dream-html that referenced this issue Dec 22, 2024
However depending on aantron/dream#371 this
may be removed from here and put in upstream.
@yawaramin
Copy link
Contributor Author

OK so I see Dream.scope "/" ... tested here

Dream.scope "/" [pipeline] [
so I assume it's a supported use case...in that case maybe my use suggestion has less weight since it's already so simple. Hmm 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant