feat: 添加 swagger 配置
This commit is contained in:
parent
84e4c68413
commit
554614aa4a
|
@ -8,6 +8,7 @@
|
||||||
<PackageReference Include="BootstrapBlazor.Middleware" Version="1.0.0" />
|
<PackageReference Include="BootstrapBlazor.Middleware" Version="1.0.0" />
|
||||||
<PackageReference Include="Longbow.Logging" Version="6.0.2" />
|
<PackageReference Include="Longbow.Logging" Version="6.0.2" />
|
||||||
<PackageReference Include="Longbow.Tasks" Version="5.2.1" />
|
<PackageReference Include="Longbow.Tasks" Version="5.2.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
namespace Microsoft.AspNetCore.Builder;
|
using BootstrapAdmin.Web;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
@ -20,6 +22,8 @@ public static class ApplicationBuilderExtensions
|
||||||
builder.UseAuthentication();
|
builder.UseAuthentication();
|
||||||
builder.UseAuthorization();
|
builder.UseAuthorization();
|
||||||
|
|
||||||
|
builder.UseSwagger(builder.Configuration["SwaggerPathBase"].TrimEnd('/'));
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using BootstrapAdmin.Web.Core.Services;
|
using BootstrapAdmin.Web;
|
||||||
|
using BootstrapAdmin.Web.Core.Services;
|
||||||
using BootstrapAdmin.Web.HealthChecks;
|
using BootstrapAdmin.Web.HealthChecks;
|
||||||
using BootstrapAdmin.Web.Services;
|
using BootstrapAdmin.Web.Services;
|
||||||
using BootstrapAdmin.Web.Services.SMS;
|
using BootstrapAdmin.Web.Services.SMS;
|
||||||
|
@ -23,6 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
services.AddLogging(logging => logging.AddFileLogger().AddCloudLogger().AddDBLogger(ExceptionsHelper.Log));
|
services.AddLogging(logging => logging.AddFileLogger().AddCloudLogger().AddDBLogger(ExceptionsHelper.Log));
|
||||||
services.AddCors();
|
services.AddCors();
|
||||||
services.AddResponseCompression();
|
services.AddResponseCompression();
|
||||||
|
services.AddControllers();
|
||||||
|
|
||||||
// 增加后台任务
|
// 增加后台任务
|
||||||
services.AddTaskServices();
|
services.AddTaskServices();
|
||||||
|
@ -40,6 +42,8 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
// 增加认证授权服务
|
// 增加认证授权服务
|
||||||
services.AddBootstrapAdminSecurity<AdminService>();
|
services.AddBootstrapAdminSecurity<AdminService>();
|
||||||
|
|
||||||
|
services.AddSwagger();
|
||||||
|
|
||||||
// 增加 BootstrapApp 上下文服务
|
// 增加 BootstrapApp 上下文服务
|
||||||
services.AddScoped<BootstrapAppContext>();
|
services.AddScoped<BootstrapAppContext>();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace BootstrapAdmin.Web
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Swagger 扩展方法
|
||||||
|
/// </summary>
|
||||||
|
internal static class SwaggerExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Swagger 中间件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="app"></param>
|
||||||
|
/// <param name="pathBase"></param>
|
||||||
|
public static void UseSwagger(this IApplicationBuilder app, string pathBase)
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注入 Swagger 服务到容器内
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
public static void AddSwagger(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSwaggerGen(options =>
|
||||||
|
{
|
||||||
|
options.SwaggerDoc("v1", new OpenApiInfo
|
||||||
|
{
|
||||||
|
Version = "v1",
|
||||||
|
Title = "BootstrapAdmin API"
|
||||||
|
});
|
||||||
|
|
||||||
|
//Set the comments path for the swagger json and ui.
|
||||||
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, "BootstrapAdmin.xml");
|
||||||
|
options.IncludeXmlComments(xmlPath);
|
||||||
|
|
||||||
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Name = "Authorization",
|
||||||
|
Type = SecuritySchemeType.ApiKey,
|
||||||
|
Scheme = "Bearer",
|
||||||
|
BearerFormat = "JWT",
|
||||||
|
In = ParameterLocation.Header,
|
||||||
|
Description = "JWT Authorization header using the Bearer scheme."
|
||||||
|
});
|
||||||
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference = new OpenApiReference
|
||||||
|
{
|
||||||
|
Type = ReferenceType.SecurityScheme,
|
||||||
|
Id = "Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new string[]{ }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue