Skip to content

Commit

Permalink
Fixes #446
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Nov 26, 2020
1 parent 896da16 commit 466f1f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,10 @@ In the above scenario it is not clear which one of the two http handlers a user

If you want to learn more about `Regex` please check the [Regular Expression Language Reference](https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference).

#### routexp

The `routexp` http handler is a combination of `routex` and `routef`. It resolves a route exactly like `routex`, but then passes the resolved Regex Groups as a `Seq<string>` parameter into the supplied handler function similar to how `routef` invokes the next handler in the pipeline.

#### routeCix

The `routeCix` http handler is the case insensitive version of `routex`:
Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release Notes

- Fixed pre-conditions validation issue (see [#424](https://github.com/giraffe-fsharp/Giraffe/issues/424))
- Fixed parsing issue with Guids and ShortIds in `Giraffe.EndpointRouting` (see [#447](https://github.com/giraffe-fsharp/Giraffe/issues/447))
- Added `routexp` http handler to default router (see [#446](https://github.com/giraffe-fsharp/Giraffe/issues/446))

## 5.0.0-rc-1

Expand Down
34 changes: 28 additions & 6 deletions src/Giraffe/Routing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ let routex (path : string) : HttpHandler =
| true -> next ctx
| false -> skipPipeline

/// <summary>
/// Filters an incoming HTTP request based on the request path using Regex (case sensitive).
///
/// If the route matches the incoming HTTP request then the Regex groups will be passed into the supplied `routeHandler`.
///
/// This is similar to routex but also allows to use matched strings as parameters for a controller.
/// </summary>
/// <param name="path">Regex path.</param>
/// <param name="routeHandler">A function which accepts a string sequence of the matched groups and returns a `HttpHandler` function which will subsequently deal with the request.</param>
/// <returns>A Giraffe <see cref="HttpHandler"/> function which can be composed into a bigger web application.</returns>
let routexp (path : string) (routeHandler : seq<string> -> HttpHandler): HttpHandler =
let pattern = sprintf "^%s$" path
let regex = Regex(pattern, RegexOptions.Compiled)

fun (next : HttpFunc) (ctx : Microsoft.AspNetCore.Http.HttpContext) ->
let result = regex.Match (SubRouting.getNextPartOfPath ctx)
match result.Success with
| true ->
let args = result.Groups |> Seq.map (fun x -> x.Value)
routeHandler args next ctx
| false -> skipPipeline

/// <summary>
/// Filters an incoming HTTP request based on the request path using Regex (case insensitive).
/// </summary>
Expand All @@ -123,7 +145,7 @@ let routeCix (path : string) : HttpHandler =
/// <summary>
/// Filters an incoming HTTP request based on the request path (case sensitive).
/// If the route matches the incoming HTTP request then the arguments from the <see cref="Microsoft.FSharp.Core.PrintfFormat"/> will be automatically resolved and passed into the supplied routeHandler.
///
///
/// Supported format chars**
///
/// %b: bool
Expand All @@ -148,7 +170,7 @@ let routef (path : PrintfFormat<_,_,_,_, 'T>) (routeHandler : 'T -> HttpHandler)
/// <summary>
/// Filters an incoming HTTP request based on the request path.
/// If the route matches the incoming HTTP request then the arguments from the <see cref="Microsoft.FSharp.Core.PrintfFormat"/> will be automatically resolved and passed into the supplied routeHandler.
///
///
/// Supported format chars**
///
/// %b: bool
Expand Down Expand Up @@ -226,7 +248,7 @@ let routeStartsWithCi (subPath : string) : HttpHandler =
/// <summary>
/// Filters an incoming HTTP request based on the beginning of the request path (case sensitive).
/// If the route matches the incoming HTTP request then the arguments from the <see cref="Microsoft.FSharp.Core.PrintfFormat"/> will be automatically resolved and passed into the supplied routeHandler.
///
///
/// Supported format chars**
///
/// %b: bool
Expand Down Expand Up @@ -254,7 +276,7 @@ let routeStartsWithf (path : PrintfFormat<_,_,_,_, 'T>) (routeHandler : 'T -> Ht
/// <summary>
/// Filters an incoming HTTP request based on the beginning of the request path (case insensitive).
/// If the route matches the incoming HTTP request then the arguments from the <see cref="Microsoft.FSharp.Core.PrintfFormat"/> will be automatically resolved and passed into the supplied routeHandler.
///
///
/// Supported format chars**
///
/// %b: bool
Expand Down Expand Up @@ -312,7 +334,7 @@ let subRouteCi (path : string) (handler : HttpHandler) : HttpHandler =
/// <summary>
/// Filters an incoming HTTP request based on a part of the request path (case sensitive).
/// If the sub route matches the incoming HTTP request then the arguments from the <see cref="Microsoft.FSharp.Core.PrintfFormat"/> will be automatically resolved and passed into the supplied routeHandler.
///
///
/// Supported format chars
///
/// %b: bool
Expand All @@ -322,7 +344,7 @@ let subRouteCi (path : string) (handler : HttpHandler) : HttpHandler =
/// %d: int64
/// %f: float/double
/// %O: Guid
///
///
/// Subsequent routing handlers inside the given handler function should omit the already validated path.
/// </summary>
/// <param name="path">A format string representing the expected request sub path.</param>
Expand Down

0 comments on commit 466f1f8

Please sign in to comment.