giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.13k stars 266 forks source link

EndpointRouting - Catch All Endpoint for Custom 404 error messages #537

Open shaif-dorIT opened 1 year ago

shaif-dorIT commented 1 year ago

Hi,

I started using F# and giraffe in order to learn Functional Programming.

I think that the title says it all, but I elaborate:

In regular route with the routex you can use regex route with "/*" to get catchall route.

How can I mimic that behavior in endpoint routing so I can get a custom handler for dealing with 404 in the endpoint list that is needed in .UseEndpoints(fun e -> e.MapGiraffeEndpoints(endpoints))

64J0 commented 1 year ago

Why don't you use the routex?

esbenbjerre commented 1 year ago

Why don't you use the routex?

It looks like routex isn't supported for the endpoint router.

The documentation does state the default and endpoint router can be used side by side, but I think it would be more elegant to add support for routex and/or adding a simple wildcard router function to the endpoint router.

esbenbjerre commented 1 year ago

After some searching I saw Giraffe maps route directly to IEndpointRouteBuilder.Map[Methods] so we can just use a catch-all route template.

let health: HttpHandler = setStatusCode StatusCodes.Status200OK >=> json {| Message = "OK" |}
let notFound: HttpHandler = setStatusCode StatusCodes.Status404NotFound >=> json {| Message = "Not found" |}

let endpoints =
  [
    GET [
      route "/health" health
    ]
    route "{*url}" notFound
  ]