Add a proper Gateway service

This commit is contained in:
2025-09-23 22:56:06 +08:00
parent 4573d9395f
commit 3b3287db0b
11 changed files with 278 additions and 66 deletions

View File

@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["DysonNetwork.Gateway/DysonNetwork.Gateway.csproj", "DysonNetwork.Gateway/"]
RUN dotnet restore "DysonNetwork.Gateway/DysonNetwork.Gateway.csproj"
COPY . .
WORKDIR "/src/DysonNetwork.Gateway"
RUN dotnet build "./DysonNetwork.Gateway.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./DysonNetwork.Gateway.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DysonNetwork.Gateway.dll"]

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="9.4.2" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DysonNetwork.ServiceDefaults\DysonNetwork.ServiceDefaults.csproj" />
<ProjectReference Include="..\DysonNetwork.Shared\DysonNetwork.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,175 @@
using System.Threading.RateLimiting;
using DysonNetwork.Shared.Http;
using Microsoft.AspNetCore.RateLimiting;
using Yarp.ReverseProxy.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.ConfigureAppKestrel(builder.Configuration, maxRequestBodySize: long.MaxValue, enableGrpc: false);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.SetIsOriginAllowed(origin => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter("fixed", limiterOptions =>
{
limiterOptions.PermitLimit = 120;
limiterOptions.Window = TimeSpan.FromMinutes(1);
limiterOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
limiterOptions.QueueLimit = 0;
});
});
var routes = new[]
{
new RouteConfig()
{
RouteId = "ring-ws",
ClusterId = "ring",
Match = new RouteMatch { Path = "/ws" }
},
new RouteConfig()
{
RouteId = "ring-api",
ClusterId = "ring",
Match = new RouteMatch { Path = "/ring/{**catch-all}" },
Transforms =
[
new Dictionary<string, string> { { "PathRemovePrefix", "/ring" } },
new Dictionary<string, string> { { "PathPrefix", "/api" } }
]
},
new RouteConfig()
{
RouteId = "pass-openid",
ClusterId = "pass",
Match = new RouteMatch { Path = "/.well-known/openid-configuration" }
},
new RouteConfig()
{
RouteId = "pass-jwks",
ClusterId = "pass",
Match = new RouteMatch { Path = "/.well-known/jwks" }
},
new RouteConfig()
{
RouteId = "pass-api",
ClusterId = "pass",
Match = new RouteMatch { Path = "/id/{**catch-all}" },
Transforms =
[
new Dictionary<string, string> { { "PathRemovePrefix", "/id" } },
new Dictionary<string, string> { { "PathPrefix", "/api" } }
]
},
new RouteConfig()
{
RouteId = "drive-tus",
ClusterId = "drive",
Match = new RouteMatch { Path = "/api/tus" }
},
new RouteConfig()
{
RouteId = "drive-api",
ClusterId = "drive",
Match = new RouteMatch { Path = "/drive/{**catch-all}" },
Transforms =
[
new Dictionary<string, string> { { "PathRemovePrefix", "/drive" } },
new Dictionary<string, string> { { "PathPrefix", "/api" } }
]
},
new RouteConfig()
{
RouteId = "sphere-api",
ClusterId = "sphere",
Match = new RouteMatch { Path = "/sphere/{**catch-all}" },
Transforms =
[
new Dictionary<string, string> { { "PathRemovePrefix", "/sphere" } },
new Dictionary<string, string> { { "PathPrefix", "/api" } }
]
},
new RouteConfig()
{
RouteId = "develop-api",
ClusterId = "develop",
Match = new RouteMatch { Path = "/develop/{**catch-all}" },
Transforms =
[
new Dictionary<string, string> { { "PathRemovePrefix", "/develop" } },
new Dictionary<string, string> { { "PathPrefix", "/api" } }
]
}
};
var clusters = new[]
{
new ClusterConfig()
{
ClusterId = "ring",
Destinations = new Dictionary<string, DestinationConfig>
{
{ "destination1", new DestinationConfig() { Address = "http://ring" } }
}
},
new ClusterConfig()
{
ClusterId = "pass",
Destinations = new Dictionary<string, DestinationConfig>
{
{ "destination1", new DestinationConfig() { Address = "http://pass" } }
}
},
new ClusterConfig()
{
ClusterId = "drive",
Destinations = new Dictionary<string, DestinationConfig>
{
{ "destination1", new DestinationConfig() { Address = "http://drive" } }
}
},
new ClusterConfig()
{
ClusterId = "sphere",
Destinations = new Dictionary<string, DestinationConfig>
{
{ "destination1", new DestinationConfig() { Address = "http://sphere" } }
}
},
new ClusterConfig()
{
ClusterId = "develop",
Destinations = new Dictionary<string, DestinationConfig>
{
{ "destination1", new DestinationConfig() { Address = "http://develop" } }
}
}
};
builder.Services
.AddReverseProxy()
.LoadFromMemory(routes, clusters)
.AddServiceDiscoveryDestinationResolver();
var app = builder.Build();
app.UseCors();
app.UseRateLimiter();
app.MapReverseProxy().RequireRateLimiting("fixed");
app.Run();

View File

@@ -0,0 +1,21 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}