Fix cors and prefix rewriting

This commit is contained in:
David Fowler 2023-05-07 17:35:58 -07:00 committed by Reuben Bond
parent 6a2fceb57c
commit 561d48bc62
3 changed files with 14 additions and 13 deletions

View File

@ -6,25 +6,25 @@ internal static class Extensions
public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddReverseProxy(this IServiceCollection services, IConfiguration configuration)
{ {
// REVIEW: This should come from configuration // REVIEW: This should come from configuration
var s = new (string, string, string)[] var s = new (string, string, string, bool)[]
{ {
("c-short", "c", "catalog"), ("c-short", "c", "catalog", true),
("c-long", "catalog-api", "catalog"), ("c-long", "catalog-api", "catalog", true),
("b-short", "b", "basket"), ("b-short", "b", "basket", true),
("b-long", "basket-api", "basket"), ("b-long", "basket-api", "basket", true),
("o-short", "o", "orders"), ("o-short", "o", "orders", true) ,
("o-long", "ordering-api", "orders"), ("o-long", "ordering-api", "orders", true),
("h-long", "hub/notificationhub", "signalr") ("h-long", "hub/notificationhub", "signalr", false)
}; };
var routes = new List<RouteConfig>(); var routes = new List<RouteConfig>();
var clusters = new Dictionary<string, ClusterConfig>(); var clusters = new Dictionary<string, ClusterConfig>();
var urls = configuration.GetRequiredSection("Urls"); var urls = configuration.GetRequiredSection("Urls");
foreach (var (routeId, prefix, clusterId) in s) foreach (var (routeId, prefix, clusterId, rewritePrefix) in s)
{ {
var destination = urls.GetRequiredValue(clusterId); var destination = urls.GetRequiredValue(clusterId);
@ -36,10 +36,10 @@ internal static class Extensions
{ {
Path = $"/{prefix}/{{**catch-all}}" Path = $"/{prefix}/{{**catch-all}}"
}, },
Metadata = new Dictionary<string, string>() Metadata = rewritePrefix ? new Dictionary<string, string>()
{ {
["prefix"] = prefix ["prefix"] = prefix
} } : null
}); });
clusters[clusterId] = new() clusters[clusterId] = new()

View File

@ -27,13 +27,13 @@ app.UseServiceDefaults();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseCors(); app.UseCors("CorsPolicy");
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.MapGet("/", () => Results.Redirect("/swagger")); app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapControllers().RequireCors("CorsPolicy"); app.MapControllers();
app.MapReverseProxy(); app.MapReverseProxy();
await app.RunAsync(); await app.RunAsync();

View File

@ -1,5 +1,6 @@
{ {
"PurchaseUrl": "http://localhost:5229", "PurchaseUrl": "http://localhost:5229",
"SignalrHubUrl": "http://localhost:5229",
"IdentityUrl": "http://localhost:5223", "IdentityUrl": "http://localhost:5223",
"IdentityUrlHC": "http://localhost:5223/hc", "IdentityUrlHC": "http://localhost:5223/hc",
"CallBackUrl": "http://localhost:5100/", "CallBackUrl": "http://localhost:5100/",