diff --git a/src/ApiGateways/Yarp/ApiGateway/ApiGateway.csproj b/src/ApiGateways/Yarp/ApiGateway/ApiGateway.csproj new file mode 100644 index 000000000..432df38e3 --- /dev/null +++ b/src/ApiGateways/Yarp/ApiGateway/ApiGateway.csproj @@ -0,0 +1,17 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + diff --git a/src/ApiGateways/Yarp/ApiGateway/Program.cs b/src/ApiGateways/Yarp/ApiGateway/Program.cs new file mode 100644 index 000000000..acc40633e --- /dev/null +++ b/src/ApiGateways/Yarp/ApiGateway/Program.cs @@ -0,0 +1,71 @@ +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(); +var clusters = new Dictionary(); +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() + { + ["prefix"] = prefix + } + }); + + clusters[clusterId] = new() + { + ClusterId = clusterId, + Destinations = new Dictionary() + { + { 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(); diff --git a/src/ApiGateways/Yarp/ApiGateway/Properties/launchSettings.json b/src/ApiGateways/Yarp/ApiGateway/Properties/launchSettings.json new file mode 100644 index 000000000..4ebe30168 --- /dev/null +++ b/src/ApiGateways/Yarp/ApiGateway/Properties/launchSettings.json @@ -0,0 +1,22 @@ +{ + "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" + } + } + } +} diff --git a/src/ApiGateways/Yarp/ApiGateway/appsettings.Development.json b/src/ApiGateways/Yarp/ApiGateway/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/src/ApiGateways/Yarp/ApiGateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/ApiGateways/Yarp/ApiGateway/appsettings.json b/src/ApiGateways/Yarp/ApiGateway/appsettings.json new file mode 100644 index 000000000..61ff2f8b7 --- /dev/null +++ b/src/ApiGateways/Yarp/ApiGateway/appsettings.json @@ -0,0 +1,16 @@ +{ + "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" + } +} diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 4437cc543..b48ea8c13 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -3,7 +3,6 @@ true true - @@ -88,5 +87,6 @@ + - + \ No newline at end of file diff --git a/src/Web/WebMVC/appsettings.json b/src/Web/WebMVC/appsettings.json index e0d07178a..401ef7d8f 100644 --- a/src/Web/WebMVC/appsettings.json +++ b/src/Web/WebMVC/appsettings.json @@ -1,8 +1,5 @@ { - "PurchaseUrl": "http://localhost:5229", - "CatalogUrl": "http://localhost:5222", - "OrderingUrl": "http://localhost:5224", - "BasketUrl": "http://localhost:5221", + "PurchaseUrl": "http://localhost:5291", "IdentityUrl": "http://localhost:5223", "IdentityUrlHC": "http://localhost:5223/hc", "CallBackUrl": "http://localhost:5100/", diff --git a/src/eShopOnContainers-ServicesAndWebApps.sln b/src/eShopOnContainers-ServicesAndWebApps.sln index 65aff4d0e..b3467e5eb 100644 --- a/src/eShopOnContainers-ServicesAndWebApps.sln +++ b/src/eShopOnContainers-ServicesAndWebApps.sln @@ -122,10 +122,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{373D8AA1 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventBus.Tests", "BuildingBlocks\EventBus\EventBus.Tests\EventBus.Tests.csproj", "{95D735BE-2899-4495-BE3F-2600E93B4E3C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services.Common", "Services\Services.Common\Services.Common.csproj", "{CD430CE4-D5E0-4C96-84F5-AEC9162651B5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services.Common", "Services\Services.Common\Services.Common.csproj", "{CD430CE4-D5E0-4C96-84F5-AEC9162651B5}" 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 @@ -1532,6 +1536,54 @@ 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 @@ -1590,6 +1642,8 @@ 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}