Skip to content
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

[Alpha] Add new route functions (...WithExtensions) #634

Merged
merged 7 commits into from
Feb 11, 2025
Merged

Conversation

64J0
Copy link
Member

@64J0 64J0 commented Dec 19, 2024

Description

With this PR, I'm adding some new endpoint routing functions based on what was presented at PR #619. The new routing functions are:

  • routeWithExtensions
  • routefWithExtensions
  • subRouteWithExtensions

There is a new sample showing how to use it with ASP.NET's Rate limiting middleware, a new docs section was added to explain this new feature and more unit tests were added.

From the DOCUMENTATION (added with this PR):

ASP.NET Core provides several "extension" functions which can be used to fine-tune the HTTP handler behaviour. For example, there's the Rate limiting and Output caching middlewares.

By using the Endpoint Routing module we can leverage this along with the ...WithExtensions routing functions: routeWithExtensions, routefWithExtensions and subRouteWithExtensions.

Basically, whenever you decide to use a routing function variant with ...WithExtensions you're required to provide as the first parameter a function that obbeys the ConfigureEndpoint type definition:

// Note: IEndpointConventionBuilder is a shorter version of Microsoft.AspNetCore.Builder.IEndpointConventionBuilder
type ConfigureEndpoint = IEndpointConventionBuilder -> IEndpointConventionBuilder

And you can use it like this:

let MY_RATE_LIMITER = "fixed"

let endpoints: list<Endpoint> =
    [
        GET [
            routeWithExtensions (fun eb -> eb.RequireRateLimiting MY_RATE_LIMITER) "/rate-limit" (text "Hello World")
            route "/no-rate-limit" (text "Hello World: No Rate Limit!")
        ]
    ]

In this example, we're using the ASP.NET Rate limiting middleware for the path /rate-limit, and not using it for /no-rate-limit. If you'd like to test it, check the sample at the official repository under the path samples/RateLimiting/. There's a README.md file with instructions on how to run it locally.

Note that for those extensions to work properly, you'll probably need to make additional changes to the server. Please check the official extension documentation page to know more about this.

How to test

  • Check the CI;
  • Check the new sample.

Related issues

@64J0 64J0 changed the title Add new route functions Add new route functions (...WithExtensions) Dec 19, 2024
@64J0 64J0 changed the title Add new route functions (...WithExtensions) Add new route functions (...WithExtensions) + .NET 8 and 9 Dec 19, 2024
@64J0 64J0 self-assigned this Dec 19, 2024
@@ -156,29 +157,71 @@ module Routers =
| CONNECT -> "CONNECT"
| _ -> ""

[<RequireQualifiedAccess>]
type AspNetExtension =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan the DU. This means if someone wants to extend this functionality they have to edit giraffe source code. And if they're doing something custom it's not something we'd want to accept.

It would be better to have a list of IEndpointConventionBuilder -> IEndpointConventionBuilder/unit functions and maybe provide helpers for known ones we'd want to support.

Copy link
Member Author

@64J0 64J0 Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code to expose the type ConfigureEndpoint = IEndpointConventionBuilder -> IEndpointConventionBuilder to the client. Is this aligned to what you had in mind?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nojaf and @mrtz-j I'd like to know your opinion too

Copy link
Contributor

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From afar this looks good, I agree with Jimmy his comment.

@64J0 64J0 marked this pull request as draft December 20, 2024 11:05
@64J0 64J0 changed the title Add new route functions (...WithExtensions) + .NET 8 and 9 Add new route functions (...WithExtensions) Jan 30, 2025
@64J0 64J0 changed the title Add new route functions (...WithExtensions) [Alpha] Add new route functions (...WithExtensions) Jan 30, 2025
@64J0 64J0 force-pushed the add-new-route-fn branch from c9775ab to 35d75d6 Compare January 30, 2025 22:48
@64J0 64J0 requested review from TheAngryByrd and nojaf January 30, 2025 22:51
@64J0 64J0 marked this pull request as ready for review January 31, 2025 14:09
Copy link
Contributor

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, fine by me but I would try to get a hold of the folks who actually want this. Give them the alpha and take it from there.

RELEASE_NOTES.md Outdated
@@ -1,6 +1,27 @@
Release Notes
=============

## 8.0.0 - 202x-xx-xx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-alpha-001 or however it is done here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done!

@@ -2,7 +2,7 @@
<PropertyGroup>
<!-- General -->
<AssemblyName>Giraffe</AssemblyName>
<AssemblyVersion>7.0.2</AssemblyVersion>
<AssemblyVersion>8.0.0</AssemblyVersion>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alpha

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -267,6 +285,7 @@ module Routers =
| NestedEndpoint(t, lst, ce) -> NestedEndpoint(t, lst, ce >> f)
| MultiEndpoint(lst) -> MultiEndpoint(List.map (configureEndpoint f) lst)

// XXX should we keep this?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it used anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, but since I'm not sure why it was added in the first place, I'll leave it there and remove the comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this comment

Copy link
Member

@TheAngryByrd TheAngryByrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Agree with @nojaf. Time to let people try it and get feedback.

@64J0 64J0 merged commit 880dcd7 into master Feb 11, 2025
6 checks passed
@64J0 64J0 deleted the add-new-route-fn branch February 11, 2025 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants