diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs index 3ed7ca906..0c9aee1c2 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Extensions/Extensions.cs @@ -1,51 +1,10 @@ -using Yarp.ReverseProxy.Configuration; -using Yarp.ReverseProxy.Transforms; +using Yarp.ReverseProxy.Transforms; internal static class Extensions { public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration) { - // REVIEW: We could load the routes and clusters from configuration instead of code - // using YARP's default schema, it's slightly more verbose but also reloable. - var s = new List<(string, string, string, bool)>(); - foreach (var c in configuration.GetRequiredSection("Routes").GetChildren()) - { - s.Add((c["0"], c["1"], c["2"], c.GetValue("3", false))); - } - - var routes = new List(); - var clusters = new Dictionary(); - var urls = configuration.GetRequiredSection("Urls"); - - foreach (var (routeId, prefix, clusterId, rewritePrefix) in s) - { - var destination = urls.GetRequiredValue(clusterId); - - routes.Add(new() - { - RouteId = routeId, - ClusterId = clusterId, - Match = new() - { - Path = $"/{prefix}/{{**catch-all}}" - }, - Metadata = rewritePrefix ? new Dictionary() - { - ["prefix"] = prefix - } : null - }); - - clusters[clusterId] = new() - { - ClusterId = clusterId, - Destinations = new Dictionary() - { - { clusterId, new DestinationConfig() { Address = destination } } - } - }; - } - - services.AddReverseProxy().LoadFromMemory(routes, clusters.Values.ToList()) + services.AddReverseProxy().LoadFromConfig(configuration.GetRequiredSection("ReverseProxy")) .AddTransforms(builder => { if (builder.Route?.Metadata?["prefix"] is string prefix) diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json b/src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json index 729dd5011..f9f077bc5 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/appsettings.json @@ -27,18 +27,101 @@ "webshoppingagg": "Shopping Aggregator for Web Clients" } }, - "Routes": [ - [ "c-short", "c", "catalog", true ], - [ "c-long", "catalog-api", "catalog", true ], - [ "b-short", "b", "basket", true ], - [ "b-long", "basket-api", "basket", true ], - - [ "o-short", "o", "orders", true ], - [ "o-long", "ordering-api", "orders", true ], - - [ "h-long", "hub/notificationhub", "signalr", false ] - ], + "ReverseProxy": { + "Routes": { + "c-short": { + "ClusterId": "catalog", + "Match": { + "Path": "c/{**catch-all}" + }, + "Metadata": { + "prefix": "c" + } + }, + "c-long": { + "ClusterId": "catalog", + "Match": { + "Path": "catalog-api/{**catch-all}" + }, + "Metadata": { + "prefix": "catalog-api" + } + }, + "b-short": { + "ClusterId": "basket", + "Match": { + "Path": "b/{**catch-all}" + }, + "Metadata": { + "prefix": "b" + } + }, + "b-long": { + "ClusterId": "basket", + "Match": { + "Path": "basket-api/{**catch-all}" + }, + "Metadata": { + "prefix": "basket-api" + } + }, + "o-short": { + "ClusterId": "orders", + "Match": { + "Path": "o/{**catch-all}" + }, + "Metadata": { + "prefix": "o" + } + }, + "o-long": { + "ClusterId": "orders", + "Match": { + "Path": "ordering-api/{**catch-all}" + }, + "Metadata": { + "prefix": "ordering-api" + } + }, + "h-long": { + "ClusterId": "signalr", + "Match": { + "Path": "hub/notificationhub/{**catch-all}" + } + } + }, + "Clusters": { + "basket": { + "Destinations": { + "basket/destination0": { + "Address": "http://localhost:5221" + } + } + }, + "catalog": { + "Destinations": { + "catalog/destination0": { + "Address": "http://localhost:5222" + } + } + }, + "orders": { + "Destinations": { + "orders/destination0": { + "Address": "http://localhost:5224" + } + } + }, + "signalr": { + "Destinations": { + "signalr/destination0": { + "Address": "http://localhost:5225" + } + } + } + } + }, "Urls": { "Basket": "http://localhost:5221", "Catalog": "http://localhost:5222",