Skip to content

Commit 6674213

Browse files
Asp.Versioning & RateLimiter support added
1 parent 6ffc7d5 commit 6674213

File tree

13 files changed

+137
-3
lines changed

13 files changed

+137
-3
lines changed

Shared.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Solution>
22
<Folder Name="/.solution_items/">
33
<File Path="Directory.Build.props" />
4+
<File Path="README.md" />
45
</Folder>
56
<Folder Name="/.solution_items/.nuget/">
67
<File Path=".nuget/delist.ps1" />
@@ -36,7 +37,9 @@
3637
<Project Path="TaskHub.Shared.Bootstraper/TaskHub.Shared.Bootstraper.csproj" Id="7babb715-b9be-447f-90b4-b1b3451df9bf" />
3738
<Project Path="TaskHub.Shared.ConfigurationTools/TaskHub.Shared.ConfigurationTools.csproj" Id="0f21840b-73df-465d-a20e-45983df6e24e" />
3839
<Project Path="TaskHub.Shared.OpenTelemetry/TaskHub.Shared.OpenTelemetry.csproj" Id="b991f047-11eb-4303-9662-bc4ef0fe058f" />
40+
<Project Path="TaskHub.Shared.RateLimiter/TaskHub.Shared.RateLimiter.csproj" Id="6e4d4c7b-0df0-460e-8653-8123bc540fc6" />
3941
<Project Path="TaskHub.Shared.Redis/TaskHub.Shared.Redis.csproj" Id="5fc5938a-a4d2-4fee-99cc-5d9cf54e0645" />
4042
<Project Path="TaskHub.Shared.Swagger/TaskHub.Shared.Swagger.csproj" Id="b945e948-fe0b-4724-8f55-cbd0be83e924" />
43+
<Project Path="TaskHub.Shared.Versioning/TaskHub.Shared.Versioning.csproj" Id="f205e5d7-23fb-43ea-9007-9e9d9b9042c0" />
4144
</Folder>
4245
</Solution>

TaskHub.Shared.Bootstraper/FullHostBuilder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using TaskHub.Shared.Commands.Bus;
99
using TaskHub.Shared.Infrastructure.EfCore.Bootstrap;
1010
using TaskHub.Shared.OpenTelemetry;
11-
using TaskHub.Shared.Redis;
1211

1312
namespace TaskHub.Shared.Bootstraper;
1413

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using System.Text;
3+
using TaskHub.Shared.ConfigurationTools;
4+
using TaskHub.Shared.Versioning;
5+
6+
namespace TaskHub.Shared.Bootstraper.Settings;
7+
8+
public static class AddVersioning
9+
{
10+
public static void AddAppVersioning(this WebApplicationBuilder builder) => builder.Services.AddAppVersioning(options =>
11+
{
12+
var section = builder.Configuration.GetSection("Versioning");
13+
options.IsEnabled = section.GetBool("IsEnabled");
14+
options.AppVersion = section.GetString("AppVersion");
15+
options.Format = section.GetString("NameFormat");
16+
options.InUrl = section.GetBool("InUrl");
17+
options.ReportVersions = section.GetBool("ReportVersions");
18+
});
19+
}

TaskHub.Shared.Bootstraper/TaskHub.Shared.Bootstraper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<ProjectReference Include="..\TaskHub.Shared.Redis\TaskHub.Shared.Redis.csproj" />
1515
<ProjectReference Include="..\TaskHub.Shared.Response\TaskHub.Shared.Response.csproj" />
1616
<ProjectReference Include="..\TaskHub.Shared.Swagger\TaskHub.Shared.Swagger.csproj" />
17+
<ProjectReference Include="..\TaskHub.Shared.Versioning\TaskHub.Shared.Versioning.csproj" />
1718
</ItemGroup>
1819
</Project>

TaskHub.Shared.Commands.Abstractions/Behavior/IPostBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace TaskHub.Shared.Commands.Abstractions.Behavior;
55

6-
public interface IPostBehavior<in TCommand, in TResult>
6+
public interface IPostBehavior<in TCommand, in TResult>
77
where TCommand : ICommand
88
where TResult : Result
99
{

TaskHub.Shared.OpenTelemetry/TaskHub.Shared.OpenTelemetry.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.13.0" />
1111
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.13.0" />
1212
</ItemGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\TaskHub.Shared.ConfigurationTools\TaskHub.Shared.ConfigurationTools.csproj" />
15+
</ItemGroup>
1316
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.RateLimiting;
2+
3+
namespace TaskHub.Shared.RateLimiter;
4+
5+
public class FixedLimiterOptions
6+
{
7+
public bool IsEnabled { get; set; } = false;
8+
public bool AutoReplenishment { get; set; } = false;
9+
public string PartitionKey { get; set; } = string.Empty;
10+
public int PermitLimit { get; set; } = 0;
11+
public int QueueLimit { get; set; } = 0;
12+
public int Window { get; set; } = 0;
13+
public int ProcessingOrder { get; set; } = 0;
14+
15+
public QueueProcessingOrder QueueProcessingOrder => ProcessingOrder switch
16+
{
17+
0 => QueueProcessingOrder.OldestFirst,
18+
1 => QueueProcessingOrder.NewestFirst,
19+
_ => QueueProcessingOrder.OldestFirst
20+
};
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using System.Threading.RateLimiting;
5+
6+
namespace TaskHub.Shared.RateLimiter;
7+
8+
public static class RateLimiterBootstrap
9+
{
10+
public static void AddFixedRateLimiter(this IServiceCollection services, Action<FixedLimiterOptions> action)
11+
{
12+
var options = new FixedLimiterOptions();
13+
action(options);
14+
if (options.IsEnabled) services.AddRateLimiter(o =>
15+
o.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => RateLimitPartition.GetFixedWindowLimiter(
16+
partitionKey: options.PartitionKey, factory: partition => new FixedWindowRateLimiterOptions
17+
{
18+
AutoReplenishment = options.AutoReplenishment,
19+
PermitLimit = options.PermitLimit,
20+
QueueLimit = options.QueueLimit,
21+
QueueProcessingOrder = options.QueueProcessingOrder,
22+
Window = TimeSpan.FromMinutes(options.Window)
23+
})
24+
));
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<ItemGroup>
3+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.3.0" />
4+
<PackageReference Include="Microsoft.AspNetCore.RateLimiting" Version="7.0.0-rc.2.22476.2" />
5+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.10" />
6+
<PackageReference Include="System.Threading.RateLimiting" Version="9.0.10" />
7+
</ItemGroup>
8+
</Project>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.10" />
4-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.10" />
54
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.10" />
65
</ItemGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="..\TaskHub.Shared.ConfigurationTools\TaskHub.Shared.ConfigurationTools.csproj" />
8+
</ItemGroup>
79
</Project>

0 commit comments

Comments
 (0)