anuragsoni / routes

typed bidirectional router for OCaml/ReasonML web applications
https://anuragsoni.github.io/routes/
BSD 3-Clause "New" or "Revised" License
145 stars 11 forks source link

Good way to match on methods and paths? #153

Closed mooreryan closed 8 months ago

mooreryan commented 8 months ago

(This is more of a question, so perhaps it would be better on discuss forum or something like that.)

I see in this pull request (https://github.com/anuragsoni/routes/pull/92/files) that there was once support for also matching on http method/verb. What is the recommended way to handle the http verbs now?

I have a bunch of routes that look like this:

let about = 
  let path = s "about" /? nil in
  let handler = function
    | `GET -> Server.respond ...
    | _ -> Server.respond `Method_not_allowed 
  in
  path @--> handler

then all those go into one router with one_of and then matching done with the match'. That's okay, but it's a bit backwards from what I might expect (eg ASP.NET minimal api).

Example

```csharp app.MapGet("/", () => "This is a GET"); app.MapPost("/", () => "This is a POST"); app.MapPut("/", () => "This is a PUT"); app.MapDelete("/", () => "This is a DELETE"); ```

Alternatively, I could imagine something where you might have a associative map that maps verbs to a router (each set up with one_of, then you have some set up where you match first on the verb, and then do the route matching with match'.

I suppose it is a mostly matter of personal preference, but I wanted to get your input since at one time this library also handled the http verbs.