SuaveIO / suave

Suave is a simple web development F# library providing a lightweight web server and a set of combinators to manipulate route flow and task composition.
https://suave.io
Other
1.32k stars 198 forks source link

Built in way to build nested routes? #740

Closed njlr closed 4 years ago

njlr commented 4 years ago

Suppose I divide my app into "regular" routes and "api" routes.

let api : WebPart = ... 

let regular : WebPart = ...

Then my app is a combination of the two:

let app = 
  choose 
    [
      path "/api" api
      regular
    ]

The problem with this is that my api routes must be defined with /api in front:

let api = 
  choose
    [
      path "/api/products" ... 
      path "/api/users" ...
    ]

But I would prefer if the root of my api were defined outside and write my api like this:

let api = 
  choose
    [
      path "/products" ... 
      path "/users" ...
    ]

I wrote a quick combinator that snips off the path before sending it on:

let takePath (path : string) (next : WebPart) : WebPart =
  fun context -> async {
    if context.request.path.StartsWith path
    then
      return!
        {
          context with
            request =
              {
                context.request with
                  rawPath = context.request.path.Substring (path.Length)
              }
        }
        |> next
    else
      return None
  }

However, this seems like the sort of thing that would already be built in.

Is this implemented in Suave?

ademar commented 4 years ago

No, we do not have this implemented. There have been some discussions and proposals, you can find them in past issues.

https://github.com/SuaveIO/suave/issues/570

njlr commented 4 years ago

Thanks, didn't see those.

I will close my issue.