使用 OpenAPI 规范生成 API 文档

This commit is contained in:
hello 2024-03-03 11:11:18 +08:00
parent 2e57e89aba
commit 2f79bf6b25
5 changed files with 45 additions and 7 deletions

View File

@ -1,15 +1,15 @@
var builder = DistributedApplication.CreateBuilder(args);
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("helloshop.apiservice");
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice");
builder.AddProject<Projects.HelloShop_WebApp>("helloshop.webfrontend").WithReference(apiservice);
builder.AddProject<Projects.HelloShop_IdentityService>("identityservice");
builder.AddProject<Projects.HelloShop_IdentityService>("helloshop.identityservice");
builder.AddProject<Projects.HelloShop_OrderingService>("orderingservice");
builder.AddProject<Projects.HelloShop_OrderingService>("helloshop.orderingservice");
builder.AddProject<Projects.HelloShop_ProductService>("productservice");
builder.AddProject<Projects.HelloShop_ProductService>("helloshop.productservice");
builder.AddProject<Projects.HelloShop_BasketService>("basketservice");
builder.AddProject<Projects.HelloShop_BasketService>("helloshop.basketservice");
builder.AddProject<Projects.HelloShop_WebApp>("webapp").WithReference(apiservice);
builder.Build().Run();

View File

@ -11,7 +11,6 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HelloShop.ServiceDefaults\HelloShop.ServiceDefaults.csproj" />

View File

@ -21,6 +21,7 @@ builder.Services.AddDbContext<IdentityServiceDbContext>(options =>
});
builder.Services.AddDataSeedingProviders();
builder.Services.AddOpenApi();
var app = builder.Build();
@ -40,5 +41,6 @@ app.UseAuthorization();
app.MapControllers();
app.UseDataSeedingProviders();
app.UseOpenApi();
app.Run();

View File

@ -0,0 +1,36 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace HelloShop.ServiceDefaults.Extensions
{
public static class OpenApiExtensions
{
public static IServiceCollection AddOpenApi(this IServiceCollection services, Action<SwaggerGenOptions>? configureOptions = null)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(configureOptions);
return services;
}
public static IApplicationBuilder UseOpenApi(this IApplicationBuilder app, Action<SwaggerOptions>? apiConfigureOptions = null, Action<SwaggerUIOptions>? uiConfigureOptions = null)
{
app.UseSwagger(apiConfigureOptions);
app.UseSwaggerUI(uiConfigureOptions);
return app;
}
}
}

View File

@ -16,5 +16,6 @@
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.7.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>