聚合 OpenApi 文档

This commit is contained in:
hello 2024-04-20 22:15:14 +08:00
parent 6f4a338bb4
commit f13e775ca1
9 changed files with 1873 additions and 10 deletions

View File

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc;
namespace HelloShop.ApiService.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public string GetHelloWorld()
{
return "Hello World!";
}
}

View File

@ -0,0 +1,36 @@
using HelloShop.ApiService.Infrastructure;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerUI;
namespace HelloShop.ApiService.Extensions;
public class OpenApiConfigureOptions(IConfiguredServiceEndPointResolver serviceResolver, HttpClient httpClient) : IConfigureOptions<SwaggerUIOptions>
{
public void Configure(SwaggerUIOptions options)
{
List<UrlDescriptor> urlDescriptors = [];
foreach (var serviceEndpoint in serviceResolver.GetConfiguredServiceEndpointsAsync().GetAwaiter().GetResult())
{
foreach (var endPoint in serviceEndpoint.Endpoints ?? [])
{
UriBuilder uriBuilder = new(endPoint) { Path = "swagger/v1/swagger.json" };
HttpResponseMessage response = httpClient.GetAsync(uriBuilder.Uri).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
urlDescriptors.Add(new UrlDescriptor
{
Url = uriBuilder.Uri.ToString(),
Name = serviceEndpoint.ServiceName
});
break;
}
}
}
options.ConfigObject.Urls = urlDescriptors;
options.SwaggerEndpoint("v1/swagger.json", "apiservice");
}
}

View File

@ -1,5 +1,9 @@
using HelloShop.ApiService.Infrastructure;
using Yarp.ReverseProxy.Configuration;
using HelloShop.ServiceDefaults.Extensions;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerUI;
using HelloShop.ApiService.Extensions;
var builder = WebApplication.CreateBuilder(args);
@ -17,6 +21,9 @@ builder.Services.AddReverseProxy()
builder.Services.AddSingleton<IConfiguredServiceEndPointResolver, ConfiguredServiceEndPointResolver>();
builder.Services.AddSingleton<IReverseProxyConfigProvider, CustomReverseProxyConfigProvider>();
builder.Services.AddTransient<IConfigureOptions<SwaggerUIOptions>, OpenApiConfigureOptions>();
builder.Services.AddOpenApi();
var app = builder.Build();
@ -34,4 +41,6 @@ app.MapReverseProxy();
app.MapControllers();
app.UseOpenApi();
app.Run();

View File

@ -68,6 +68,8 @@ app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.MapControllers();
app.UseDataSeedingProviders();

View File

@ -1,3 +1,5 @@
using HelloShop.ServiceDefaults.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
@ -5,25 +7,20 @@ builder.AddServiceDefaults();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapDefaultEndpoints();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.MapControllers();
app.UseOpenApi();
app.Run();

View File

@ -33,6 +33,8 @@ app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.MapControllers();
// Configure extensions request pipeline.

View File

@ -4,6 +4,7 @@
using HelloShop.ServiceDefaults.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
@ -11,6 +12,7 @@ using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@ -24,6 +26,12 @@ namespace HelloShop.ServiceDefaults.Extensions
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(configureOptions);
services.Configure<SwaggerUIOptions>(options =>
{
options.DocumentTitle = Assembly.GetExecutingAssembly().GetName().Name;
options.InjectStylesheet("/ServiceDefaults/Resources/OpenApi/Custom.css");
});
services.Configure<SwaggerGenOptions>(options =>
{
options.OperationFilter<AcceptLanguageHeaderOperationFilter>();
@ -56,6 +64,11 @@ namespace HelloShop.ServiceDefaults.Extensions
app.UseSwagger(apiConfigureOptions);
app.UseSwaggerUI(uiConfigureOptions);
app.Map("/ServiceDefaults", appBuilder => appBuilder.UseStaticFiles(new StaticFileOptions
{
FileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly())
}));
return app;
}
}

View File

@ -18,4 +18,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\OpenApi\Custom.css" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff