更新nuget&&添加swagger
This commit is contained in:
+44
-16
@@ -1,6 +1,7 @@
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Domain.System;
|
||||
using Jiaowu.Api.Infrastructure.BackgroundJobs;
|
||||
using Jiaowu.Api.Infrastructure.Grades;
|
||||
using Jiaowu.Api.Infrastructure.Configuration;
|
||||
@@ -23,10 +24,11 @@ using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Microsoft.OpenApi;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Resources;
|
||||
using OpenTelemetry.Trace;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using System.Threading.RateLimiting;
|
||||
|
||||
EnvironmentFile.Load();
|
||||
@@ -62,6 +64,13 @@ if (confirmProductionDemoData && !seedDemoData)
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var swaggerDocumentVersion = typeof(Program).Assembly
|
||||
.GetCustomAttributes(
|
||||
typeof(System.Reflection.AssemblyMetadataAttribute),
|
||||
inherit: false)
|
||||
.OfType<System.Reflection.AssemblyMetadataAttribute>()
|
||||
.SingleOrDefault(x => x.Key == "SwaggerDocumentVersion")?.Value
|
||||
?? "v1";
|
||||
|
||||
if (seedDemoData && builder.Environment.IsDevelopment())
|
||||
{
|
||||
@@ -608,10 +617,10 @@ builder.Services.AddControllers()
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo
|
||||
options.SwaggerDoc(swaggerDocumentVersion, new OpenApiInfo
|
||||
{
|
||||
Title = "大学教务管理系统 API",
|
||||
Version = "v1"
|
||||
Version = swaggerDocumentVersion
|
||||
});
|
||||
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
@@ -621,17 +630,10 @@ builder.Services.AddSwaggerGen(options =>
|
||||
BearerFormat = "JWT",
|
||||
In = ParameterLocation.Header
|
||||
});
|
||||
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
options.AddSecurityRequirement(_ => new OpenApiSecurityRequirement
|
||||
{
|
||||
[
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
}
|
||||
}
|
||||
new OpenApiSecuritySchemeReference("Bearer", null, null)
|
||||
] = []
|
||||
});
|
||||
});
|
||||
@@ -640,11 +642,37 @@ var app = builder.Build();
|
||||
|
||||
app.UseExceptionHandler();
|
||||
app.UseResponseCompression();
|
||||
if (app.Environment.IsDevelopment())
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
if (context.Request.Path.StartsWithSegments("/swagger"))
|
||||
{
|
||||
var isEnabled = await context.RequestServices
|
||||
.GetRequiredService<AppDbContext>()
|
||||
.SystemFeatureSettings
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Key == SystemFeatureKeys.SwaggerDocumentation)
|
||||
.Select(x => (bool?)x.IsEnabled)
|
||||
.SingleOrDefaultAsync(context.RequestAborted) ?? false;
|
||||
if (!isEnabled)
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint(
|
||||
$"/swagger/{swaggerDocumentVersion}/swagger.json",
|
||||
$"大学教务管理系统 API {swaggerDocumentVersion}");
|
||||
options.DocExpansion(DocExpansion.None);
|
||||
options.DefaultModelsExpandDepth(-1);
|
||||
options.DefaultModelExpandDepth(1);
|
||||
options.EnableFilter();
|
||||
});
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles(new StaticFileOptions
|
||||
|
||||
Reference in New Issue
Block a user