Use the default YARP configuration

This commit is contained in:
David Fowler 2023-05-07 18:12:17 -07:00 committed by Reuben Bond
parent 758d4bbe88
commit 69476a3175
2 changed files with 96 additions and 54 deletions

View File

@ -1,51 +1,10 @@
using Yarp.ReverseProxy.Configuration; using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms;
internal static class Extensions internal static class Extensions
{ {
public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration)
{ {
// REVIEW: We could load the routes and clusters from configuration instead of code services.AddReverseProxy().LoadFromConfig(configuration.GetRequiredSection("ReverseProxy"))
// 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<RouteConfig>();
var clusters = new Dictionary<string, ClusterConfig>();
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<string, string>()
{
["prefix"] = prefix
} : null
});
clusters[clusterId] = new()
{
ClusterId = clusterId,
Destinations = new Dictionary<string, DestinationConfig>()
{
{ clusterId, new DestinationConfig() { Address = destination } }
}
};
}
services.AddReverseProxy().LoadFromMemory(routes, clusters.Values.ToList())
.AddTransforms(builder => .AddTransforms(builder =>
{ {
if (builder.Route?.Metadata?["prefix"] is string prefix) if (builder.Route?.Metadata?["prefix"] is string prefix)

View File

@ -27,18 +27,101 @@
"webshoppingagg": "Shopping Aggregator for Web Clients" "webshoppingagg": "Shopping Aggregator for Web Clients"
} }
}, },
"Routes": [
[ "c-short", "c", "catalog", true ],
[ "c-long", "catalog-api", "catalog", true ],
[ "b-short", "b", "basket", true ], "ReverseProxy": {
[ "b-long", "basket-api", "basket", true ], "Routes": {
"c-short": {
[ "o-short", "o", "orders", true ], "ClusterId": "catalog",
[ "o-long", "ordering-api", "orders", true ], "Match": {
"Path": "c/{**catch-all}"
[ "h-long", "hub/notificationhub", "signalr", false ] },
], "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": { "Urls": {
"Basket": "http://localhost:5221", "Basket": "http://localhost:5221",
"Catalog": "http://localhost:5222", "Catalog": "http://localhost:5222",