-
Notifications
You must be signed in to change notification settings - Fork 265
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
Changes from 6 commits
2effac2
ae3ac54
b33b600
1b8cfd8
2fec161
35d75d6
ed032a1
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
open System | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.AspNetCore.Builder | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Hosting | ||
open Giraffe | ||
open Giraffe.EndpointRouting | ||
open Microsoft.AspNetCore.RateLimiting | ||
open System.Threading.RateLimiting | ||
|
||
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!") | ||
] | ||
] | ||
|
||
let notFoundHandler = text "Not Found" |> RequestErrors.notFound | ||
|
||
let configureApp (appBuilder: IApplicationBuilder) = | ||
appBuilder.UseRouting().UseRateLimiter().UseGiraffe(endpoints).UseGiraffe(notFoundHandler) | ||
|
||
let configureServices (services: IServiceCollection) = | ||
// From https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-8.0#fixed-window-limiter | ||
let configureRateLimiter (rateLimiterOptions: RateLimiterOptions) = | ||
rateLimiterOptions.RejectionStatusCode <- StatusCodes.Status429TooManyRequests | ||
|
||
rateLimiterOptions.AddFixedWindowLimiter( | ||
policyName = MY_RATE_LIMITER, | ||
configureOptions = | ||
(fun (options: FixedWindowRateLimiterOptions) -> | ||
options.PermitLimit <- 10 | ||
options.Window <- TimeSpan.FromSeconds(int64 12) | ||
options.QueueProcessingOrder <- QueueProcessingOrder.OldestFirst | ||
options.QueueLimit <- 1 | ||
) | ||
) | ||
|> ignore | ||
|
||
services.AddRateLimiter(configureRateLimiter).AddRouting().AddGiraffe() | ||
|> ignore | ||
|
||
[<EntryPoint>] | ||
let main args = | ||
let builder = WebApplication.CreateBuilder(args) | ||
configureServices builder.Services | ||
|
||
let app = builder.Build() | ||
|
||
configureApp app | ||
app.Run() | ||
|
||
0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Rate Limiting Sample | ||
|
||
This sample project shows how one can configure ASP.NET's built-in [rate limiting middleware](https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-8.0). | ||
|
||
Notice that this rate limiting configuration is very simple, and for real life scenarios you'll need to figure out what is the best strategy to use for your server. | ||
|
||
To make it easier to test this project locally, and see the rate limiting middleware working, you can use the `rate-limiting-test.fsx` script: | ||
|
||
```bash | ||
# start the server | ||
dotnet run . | ||
# if you want to keep using the same terminal, just start this process in the background | ||
|
||
# then, you can use this script to test the server, and confirm that the rate-limiting | ||
# middleware is really working | ||
dotnet fsi rate-limiting-test.fsx | ||
|
||
# to run with the DEBUG flag active | ||
dotnet fsi --define:DEBUG rate-limiting-test.fsx | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../src/Giraffe/Giraffe.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
open System | ||
open System.Net.Http | ||
|
||
let request = new HttpClient(BaseAddress = new Uri("http://localhost:5000")) | ||
|
||
let program () = | ||
async { | ||
let! reqResult1 = | ||
seq { 1..100 } | ||
|> Seq.map (fun _ -> request.GetAsync "/no-rate-limit" |> Async.AwaitTask) | ||
|> Async.Parallel | ||
|
||
reqResult1 | ||
#if DEBUG | ||
|> Seq.iteri (fun i response -> | ||
printfn "\nResponse %i status code: %A" i response.StatusCode | ||
|
||
let responseReader = new StreamReader(response.Content.ReadAsStream()) | ||
printfn "Response %i content: %A" i (responseReader.ReadToEnd()) | ||
) | ||
#else | ||
|> Seq.groupBy (fun response -> response.StatusCode) | ||
|> Seq.iter (fun (group) -> | ||
let key, seqRes = group | ||
printfn "Quantity of requests with status code %A: %i" (key) (Seq.length seqRes) | ||
) | ||
#endif | ||
|
||
printfn "\nWith rate limit now...\n" | ||
|
||
let! reqResult2 = | ||
seq { 1..100 } | ||
|> Seq.map (fun _ -> request.GetAsync "/rate-limit" |> Async.AwaitTask) | ||
|> Async.Parallel | ||
|
||
reqResult2 | ||
#if DEBUG | ||
|> Seq.iteri (fun i response -> | ||
printfn "\nResponse %i status code: %A" i response.StatusCode | ||
|
||
let responseReader = new StreamReader(response.Content.ReadAsStream()) | ||
printfn "Response %i content: %A" i (responseReader.ReadToEnd()) | ||
) | ||
#else | ||
|> Seq.groupBy (fun response -> response.StatusCode) | ||
|> Seq.iter (fun (group) -> | ||
let key, seqRes = group | ||
printfn "Quantity of requests with status code %A: %i\n" (key) (Seq.length seqRes) | ||
) | ||
#endif | ||
} | ||
|
||
#time | ||
|
||
program () |> Async.RunSynchronously | ||
|
||
#time |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,19 +232,37 @@ module Routers = | |
let TRACE = applyHttpVerbToEndpoints TRACE | ||
let CONNECT = applyHttpVerbToEndpoints CONNECT | ||
|
||
let routeWithExtensions (configureEndpoint: ConfigureEndpoint) (path: string) (handler: HttpHandler) : Endpoint = | ||
SimpleEndpoint(HttpVerb.NotSpecified, path, handler, configureEndpoint) | ||
|
||
let route (path: string) (handler: HttpHandler) : Endpoint = | ||
SimpleEndpoint(HttpVerb.NotSpecified, path, handler, id) | ||
routeWithExtensions (id) (path) (handler) | ||
|
||
let routef (path: PrintfFormat<_, _, _, _, 'T>) (routeHandler: 'T -> HttpHandler) : Endpoint = | ||
let routefWithExtensions | ||
(configureEndpoint: ConfigureEndpoint) | ||
(path: PrintfFormat<_, _, _, _, 'T>) | ||
(routeHandler: 'T -> HttpHandler) | ||
: Endpoint = | ||
let template, mappings = RouteTemplateBuilder.convertToRouteTemplate path | ||
|
||
let boxedHandler (o: obj) = | ||
let t = o :?> 'T | ||
routeHandler t | ||
|
||
TemplateEndpoint(HttpVerb.NotSpecified, template, mappings, boxedHandler, id) | ||
TemplateEndpoint(HttpVerb.NotSpecified, template, mappings, boxedHandler, configureEndpoint) | ||
|
||
let routef (path: PrintfFormat<_, _, _, _, 'T>) (routeHandler: 'T -> HttpHandler) : Endpoint = | ||
routefWithExtensions (id) (path) (routeHandler) | ||
|
||
let subRouteWithExtensions | ||
(configureEndpoint: ConfigureEndpoint) | ||
(path: string) | ||
(endpoints: Endpoint list) | ||
: Endpoint = | ||
NestedEndpoint(path, endpoints, configureEndpoint) | ||
|
||
let subRoute (path: string) (endpoints: Endpoint list) : Endpoint = NestedEndpoint(path, endpoints, id) | ||
let subRoute (path: string) (endpoints: Endpoint list) : Endpoint = | ||
subRouteWithExtensions (id) (path) (endpoints) | ||
|
||
let rec applyBefore (httpHandler: HttpHandler) (endpoint: Endpoint) = | ||
match endpoint with | ||
|
@@ -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? | ||
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. Is it used anywhere? 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 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. 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. Removed this comment |
||
let addMetadata (metadata: obj) (endpoint: Endpoint) = | ||
endpoint |> configureEndpoint _.WithMetadata(metadata) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
<PropertyGroup> | ||
<!-- General --> | ||
<AssemblyName>Giraffe</AssemblyName> | ||
<AssemblyVersion>7.0.2</AssemblyVersion> | ||
<AssemblyVersion>8.0.0</AssemblyVersion> | ||
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. alpha 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. Done! |
||
<Description>A native functional ASP.NET Core web framework for F# developers.</Description> | ||
<Copyright>Copyright 2020 Dustin Moris Gorski</Copyright> | ||
<Authors>Dustin Moris Gorski and contributors</Authors> | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, done!