Compare commits
2 Commits
931ecbd4bc
...
e4629b1771
| Author | SHA1 | Date | |
|---|---|---|---|
| e4629b1771 | |||
| b699762c1b |
@ -0,0 +1,47 @@
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
namespace KonSoft.InternalGateway.Extensions
|
||||
{
|
||||
public static class YarpSwaggerUIBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseSwaggerUIWithYarp(this IApplicationBuilder app)
|
||||
{
|
||||
var serviceProvider = app.ApplicationServices;
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
|
||||
var proxyConfigProvider = serviceProvider.GetRequiredService<IProxyConfigProvider>();
|
||||
var yarpConfig = proxyConfigProvider.GetConfig();
|
||||
|
||||
var routedClusters = yarpConfig.Clusters
|
||||
.SelectMany(t => t.Destinations,
|
||||
(clusterId, destination) => new { clusterId.ClusterId, destination.Value });
|
||||
|
||||
var groupedClusters = routedClusters
|
||||
.GroupBy(q => q.Value.Address)
|
||||
.Select(t => t.First())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
foreach (var clusterGroup in groupedClusters)
|
||||
{
|
||||
var routeConfig = yarpConfig.Routes.FirstOrDefault(q =>
|
||||
q.ClusterId == clusterGroup.ClusterId);
|
||||
if (routeConfig == null)
|
||||
{
|
||||
logger.LogWarning($"Swagger UI: Couldn't find route configuration for {clusterGroup.ClusterId}...");
|
||||
continue;
|
||||
}
|
||||
|
||||
options.SwaggerEndpoint($"{clusterGroup.Value.Address}/swagger/v1/swagger.json", $"{routeConfig.RouteId} API");
|
||||
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
||||
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using KonSoft.InternalGateway;
|
||||
using KonSoft.InternalGateway.Extensions;
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using Serilog;
|
||||
|
||||
@ -24,6 +25,7 @@ try
|
||||
await app.InitializeApplicationAsync();
|
||||
app.MapReverseProxy();
|
||||
app.MapGet("/heath", () => "Online");
|
||||
app.UseSwaggerUIWithYarp();
|
||||
await app.RunAsync();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user