fix 聚合swagger

This commit is contained in:
2024-03-14 19:29:30 +08:00
parent 2cdba175bd
commit efbb72105c
5 changed files with 80 additions and 15 deletions

View File

@@ -10,6 +10,8 @@
<ItemGroup>
<PackageReference Include="Ocelot" Version="23.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.8" />
</ItemGroup>
</Project>

View File

@@ -1,16 +1,19 @@
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json", true, true);
// Add services to the container.
using Gateway;
builder.Services.AddOcelot();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseOcelot().Wait();
app.Run();
public class Program
{
public static void Main(string[] args)
{
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("ocelot.json", true, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build().Run();
}
}

41
Src/Gateway/Startup.cs Normal file
View File

@@ -0,0 +1,41 @@
using Microsoft.OpenApi.Models;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
namespace Gateway;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Gateway API", Version = "v1", Description = "# gateway api..." });
});
services.AddOcelot();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
var services = Configuration.GetSection("Services").Get<List<string>>();
foreach (var s in services)
{
c.SwaggerEndpoint($"/{s.ToLower()}/swagger/v1/swagger.json", s);
}
});
app.UseOcelot().Wait();
}
}

View File

@@ -5,5 +5,13 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Services": [
"Basic",
"Common",
"Canteen",
"Scheduled",
"User",
"Sys"
]
}

View File

@@ -84,6 +84,17 @@
"Port": 31007
}
]
},
{
"UpstreamPathTemplate": "/{url}/swagger/v1/swagger.json",
"DownstreamPathTemplate": "/{url}/swagger/v1/swagger.json",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "yikatong.isawesome.cn",
"Port": 443
}
]
}
],
"GlobalConfiguration": {}