-
Notifications
You must be signed in to change notification settings - Fork 267
feat: add routeBind support to Giraffe.EndpointRouting #709
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
Changes from 2 commits
1b89b8f
017ecf0
1089d33
6da9546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,6 +150,8 @@ module private RequestDelegateBuilder = | |
|
|
||
| [<AutoOpen>] | ||
| module Routers = | ||
| open System.Collections.Generic | ||
| open Microsoft.Extensions.Primitives | ||
|
|
||
| type HttpVerb = | ||
| | GET | ||
|
|
@@ -274,6 +276,30 @@ module Routers = | |
| let routef (path: PrintfFormat<_, _, _, _, 'T>) (routeHandler: 'T -> HttpHandler) : Endpoint = | ||
| routefWithExtensions (id) (path) (routeHandler) | ||
|
|
||
| let routeBindWithExtensions<'T> | ||
| (configureEndpoint: ConfigureEndpoint) | ||
| (path: string) | ||
| (routeHandler: 'T -> HttpHandler) | ||
| : Endpoint = | ||
|
|
||
| let bindRouteHandler (handler: 'T -> HttpHandler) : HttpHandler = | ||
| fun next ctx -> | ||
| let routeData = | ||
| ctx.GetRouteData().Values | ||
| |> Seq.map (fun kvp -> KeyValuePair(kvp.Key, StringValues(kvp.Value :?> string))) | ||
| |> fun kvps -> Dictionary<string, StringValues>(kvps) :> IDictionary<string, StringValues> | ||
|
|
||
| match ModelParser.tryParse<'T> None routeData with | ||
| | Ok model -> handler model next ctx | ||
| | Error _ -> RequestErrors.BAD_REQUEST "Failed to bind route parameters" next ctx | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it's better to not return this Instead, we need to just keep going, testing other routes, and eventually using the error handler specified by the client.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replaced the error with skipPipeline, which allows the request to flow to the next handler, but it does not switch to another endpoint because the model parsing happens after endpoint selection. To achieve the desired behavior, the model parsing would need to run before the endpoint selection phase. Based on the ASP.NET Core routing pipeline, this seems only possible by using a custom MatcherPolicy. If there is any alternative or recommended approach to achieve this, please let me know if I am missing something.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thinking out loud. Something we do in other places of the project is to let our clients define a special error handler. For example: #691. Perhaps it makes sense to use this approach here. Anyway, we can add this optional error handler later. |
||
|
|
||
| let newHandler = (bindRouteHandler routeHandler) | ||
|
|
||
| SimpleEndpoint(HttpVerb.NotSpecified, path, newHandler, configureEndpoint) | ||
|
|
||
| let routeBind<'T> (path: string) (routeHandler: 'T -> HttpHandler) : Endpoint = | ||
| routeBindWithExtensions<'T> (id) (path) (routeHandler) | ||
|
|
||
| let subRouteWithExtensions | ||
| (configureEndpoint: ConfigureEndpoint) | ||
| (path: string) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.