Browse Source

Add YARP to the BFF directly.

- This avoids the extra process hop
davidfowl/common-services
David Fowler 1 year ago
committed by Reuben Bond
parent
commit
c3efea0293
11 changed files with 70 additions and 190 deletions
  1. +65
    -1
      src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs
  2. +2
    -0
      src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs
  3. +1
    -0
      src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj
  4. +1
    -0
      src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json
  5. +0
    -17
      src/ApiGateways/Yarp/ApiGateway/ApiGateway.csproj
  6. +0
    -71
      src/ApiGateways/Yarp/ApiGateway/Program.cs
  7. +0
    -22
      src/ApiGateways/Yarp/ApiGateway/Properties/launchSettings.json
  8. +0
    -8
      src/ApiGateways/Yarp/ApiGateway/appsettings.Development.json
  9. +0
    -16
      src/ApiGateways/Yarp/ApiGateway/appsettings.json
  10. +1
    -1
      src/Web/WebMVC/appsettings.json
  11. +0
    -54
      src/eShopOnContainers-ServicesAndWebApps.sln

+ 65
- 1
src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs View File

@ -1,5 +1,69 @@
internal static class Extensions
using Yarp.ReverseProxy.Configuration;
using Yarp.ReverseProxy.Transforms;
internal static class Extensions
{
public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration)
{
// REVIEW: This should come from configuration
var s = new (string, string, string)[]
{
("c-short", "c", "catalog"),
("c-long", "catalog-api", "catalog"),
("b-short", "b", "basket"),
("b-long", "basket-api", "basket"),
("o-short", "o", "orders"),
("o-long", "ordering-api", "orders"),
("h-long", "hub/notificationhub", "signalr")
};
var routes = new List<RouteConfig>();
var clusters = new Dictionary<string, ClusterConfig>();
var urls = configuration.GetRequiredSection("Urls");
foreach (var (routeId, prefix, clusterId) in s)
{
var destination = urls.GetRequiredValue(clusterId);
routes.Add(new()
{
RouteId = routeId,
ClusterId = clusterId,
Match = new()
{
Path = $"/{prefix}/{{**catch-all}}"
},
Metadata = new Dictionary<string, string>()
{
["prefix"] = prefix
}
});
clusters[clusterId] = new()
{
ClusterId = clusterId,
Destinations = new Dictionary<string, DestinationConfig>()
{
{ clusterId, new DestinationConfig() { Address = destination } }
}
};
}
services.AddReverseProxy().LoadFromMemory(routes, clusters.Values.ToList())
.AddTransforms(builder =>
{
if (builder.Route?.Metadata?["prefix"] is string prefix)
{
builder.AddPathRemovePrefix($"/{prefix}");
}
});
return services;
}
public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
{
services.AddHealthChecks()


+ 2
- 0
src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs View File

@ -2,6 +2,7 @@
builder.AddServiceDefaults();
builder.Services.AddReverseProxy(builder.Configuration);
builder.Services.AddControllers();
builder.Services.AddHealthChecks(builder.Configuration);
@ -33,5 +34,6 @@ app.UseAuthorization();
app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapControllers().RequireCors("CorsPolicy");
app.MapReverseProxy();
await app.RunAsync();

+ 1
- 0
src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj View File

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Yarp.ReverseProxy" />
<PackageReference Include="AspNetCore.HealthChecks.Uris" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Grpc.AspNetCore.Server.ClientFactory" />


+ 1
- 0
src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json View File

@ -32,6 +32,7 @@
"Catalog": "http://localhost:5222",
"Orders": "http://localhost:5224",
"Identity": "http://localhost:5223",
"Signalr": "http://localhost:5225",
"GrpcBasket": "http://localhost:6221",
"GrpcCatalog": "http://localhost:6222",
"GrpcOrdering": "http://localhost:6224"


+ 0
- 17
src/ApiGateways/Yarp/ApiGateway/ApiGateway.csproj View File

@ -1,17 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Yarp.ReverseProxy" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Services\Services.Common\Services.Common.csproj" />
</ItemGroup>
</Project>

+ 0
- 71
src/ApiGateways/Yarp/ApiGateway/Program.cs View File

@ -1,71 +0,0 @@
using Services.Common;
using Yarp.ReverseProxy.Configuration;
using Yarp.ReverseProxy.Transforms;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
var services = new (string, string, string)[]
{
("c-short", "c", "catalog"),
("c-long", "catalog-api", "catalog"),
("b-short", "b", "basket"),
("b-long", "basket-api", "basket"),
("o-short", "o", "orders"),
("o-long", "ordering-api", "orders"),
("h-long", "hub/notificationhub", "signalr")
};
var routes = new List<RouteConfig>();
var clusters = new Dictionary<string, ClusterConfig>();
var urls = builder.Configuration.GetRequiredSection("Urls");
foreach (var (routeId, prefix, clusterId) in services)
{
var destination = urls.GetRequiredValue(clusterId);
routes.Add(new()
{
RouteId = routeId,
ClusterId = clusterId,
Match = new()
{
Path = $"/{prefix}/{{**catch-all}}"
},
Metadata = new Dictionary<string, string>()
{
["prefix"] = prefix
}
});
clusters[clusterId] = new()
{
ClusterId = clusterId,
Destinations = new Dictionary<string, DestinationConfig>()
{
{ clusterId, new DestinationConfig() { Address = destination } }
}
};
}
builder.Services.AddReverseProxy()
.LoadFromMemory(routes, clusters.Values.ToList())
.AddTransforms(builder =>
{
if (builder.Route?.Metadata?["prefix"] is string prefix)
{
builder.AddPathRemovePrefix($"/{prefix}");
}
});
var app = builder.Build();
app.UseServiceDefaults();
app.MapReverseProxy();
app.Run();

+ 0
- 22
src/ApiGateways/Yarp/ApiGateway/Properties/launchSettings.json View File

@ -1,22 +0,0 @@
{
"profiles": {
"Gateway.Http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5291",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Gateway.Https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7132;http://localhost:5291",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

+ 0
- 8
src/ApiGateways/Yarp/ApiGateway/appsettings.Development.json View File

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

+ 0
- 16
src/ApiGateways/Yarp/ApiGateway/appsettings.json View File

@ -1,16 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Urls": {
"Basket": "http://localhost:5221",
"Catalog": "http://localhost:5222",
"Orders": "http://localhost:5224",
"Identity": "http://localhost:5223",
"Signalr": "http://localhost:5225"
}
}

+ 1
- 1
src/Web/WebMVC/appsettings.json View File

@ -1,5 +1,5 @@
{
"PurchaseUrl": "http://localhost:5291",
"PurchaseUrl": "http://localhost:5229",
"IdentityUrl": "http://localhost:5223",
"IdentityUrlHC": "http://localhost:5223/hc",
"CallBackUrl": "http://localhost:5100/",


+ 0
- 54
src/eShopOnContainers-ServicesAndWebApps.sln View File

@ -126,10 +126,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services.Common", "Services
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{42B85D0F-2ED6-4C00-91FA-103DACC3D5E2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Yarp", "Yarp", "{63F5F157-504F-4CE8-86B7-0A6F087BD888}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiGateway", "ApiGateways\Yarp\ApiGateway\ApiGateway.csproj", "{98C6B254-E9D0-4401-BF7A-89E87A38B87E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@ -1536,54 +1532,6 @@ Global
{CD430CE4-D5E0-4C96-84F5-AEC9162651B5}.Release|x64.Build.0 = Release|Any CPU
{CD430CE4-D5E0-4C96-84F5-AEC9162651B5}.Release|x86.ActiveCfg = Release|Any CPU
{CD430CE4-D5E0-4C96-84F5-AEC9162651B5}.Release|x86.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|ARM.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|ARM.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|iPhone.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|x64.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|x64.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|x86.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.AppStore|x86.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|ARM.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|ARM.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|iPhone.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|x64.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|x64.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|x86.ActiveCfg = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Debug|x86.Build.0 = Debug|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|Any CPU.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|ARM.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|ARM.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|iPhone.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|iPhone.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|x64.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|x64.Build.0 = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|x86.ActiveCfg = Release|Any CPU
{98C6B254-E9D0-4401-BF7A-89E87A38B87E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -1642,8 +1590,6 @@ Global
{95D735BE-2899-4495-BE3F-2600E93B4E3C} = {373D8AA1-36BE-49EC-89F0-6CB736666285}
{CD430CE4-D5E0-4C96-84F5-AEC9162651B5} = {42B85D0F-2ED6-4C00-91FA-103DACC3D5E2}
{42B85D0F-2ED6-4C00-91FA-103DACC3D5E2} = {91CF7717-08AB-4E65-B10E-0B426F01E2E8}
{63F5F157-504F-4CE8-86B7-0A6F087BD888} = {77849D35-37D4-4802-81DC-9477B2775A40}
{98C6B254-E9D0-4401-BF7A-89E87A38B87E} = {63F5F157-504F-4CE8-86B7-0A6F087BD888}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}


Loading…
Cancel
Save