-
How to fill If it isn't possible, how to override the default behavior? I mean something like //maybe in some middleware?
if(filenameRegex.IsMatch(context.Request.Path)){
UseCluster("file-cluster");
}
else{
UseCluster("api-cluster");
} Update: var apiRegex = new Regex(@"^[a-zA-Z0-9/]+$");
app.Use(async (context, next) =>
{
var path = context.Request.Path.Value;
var isIndex = path is null or "" or "/";
var isApi = !isIndex && apiRegex.IsMatch(path);
context.ReassignProxyRequest(new ClusterState(isApi ? "api_cluster" : "file_cluster"));
await next();
}); But it throws
Is it a bug or I did something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Done. Followed the example in https://github.com/microsoft/reverse-proxy/blob/main/docs/docfx/articles/ab-testing.md here is the solution: IProxyStateLookup lookup = app.Services.GetRequiredService<IProxyStateLookup>();
var apiRegex = new Regex(@"^[a-zA-Z0-9/]+$");
string ChooseCluster(HttpContext context)
{
var path = context.Request.Path.Value;
var isIndex = path is null or "" or "/";
var isApi = !isIndex && apiRegex.IsMatch(path);
// Decide which cluster to use. This could be random, weighted, based on headers, etc.
return isApi ? "api_cluster" : "file_cluster";
}
app.MapReverseProxy(proxyPipeline => {
// Custom cluster selection
proxyPipeline.Use(async (context, next) =>
{
if(lookup.TryGetCluster(ChooseCluster(context), out var cluster))
{
context.ReassignProxyRequest(cluster);
}
await next();
});
}); And the settings: "ReverseProxy": {
"Routes": {
"route1": {
"ClusterId": "file_cluster",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"file_cluster": {
"Destinations": {
"destination1": {
"Address": "http://127.0.0.1:8080/"
}
}
},
"api_cluster": {
"Destinations": {
"destination1": {
"Address": "http://127.0.0.1:8081/"
}
}
}
}
} Note a dummy Route must be added in the settings, otherwise |
Beta Was this translation helpful? Give feedback.
Done. Followed the example in https://github.com/microsoft/reverse-proxy/blob/main/docs/docfx/articles/ab-testing.md
here is the solution: