Use the default YARP configuration
This commit is contained in:
parent
758d4bbe88
commit
69476a3175
@ -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<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())
|
||||
services.AddReverseProxy().LoadFromConfig(configuration.GetRequiredSection("ReverseProxy"))
|
||||
.AddTransforms(builder =>
|
||||
{
|
||||
if (builder.Route?.Metadata?["prefix"] is string prefix)
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user