This commit is contained in:
zpc 2026-01-14 01:00:09 +08:00
parent 981c50497d
commit 96d403a342
2 changed files with 52 additions and 45 deletions

View File

@ -1,6 +1,9 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models;
using Serilog;
using System.Reflection;
using XiangYi.AdminApi.Extensions;
using XiangYi.AdminApi.Filters;
using XiangYi.AdminApi.Middlewares;
@ -26,9 +29,9 @@ builder.Services.AddControllersWithValidation();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "相宜相亲 后台管理API",
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "相宜相亲 后台管理API",
Version = "v1",
Description = "相宜相亲后台管理系统API接口文档",
Contact = new OpenApiContact
@ -37,7 +40,7 @@ builder.Services.AddSwaggerGen(c =>
Email = "tech@xiangyi.com"
}
});
// 添加JWT认证支持到Swagger
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
@ -48,7 +51,7 @@ builder.Services.AddSwaggerGen(c =>
Scheme = "Bearer",
BearerFormat = "JWT"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
@ -108,6 +111,7 @@ builder.Services.AddRepositories();
// Add Infrastructure Services (Storage, SMS, RealName, WeChat, Redis)
builder.Services.AddInfrastructureServices(builder.Configuration);
#if DEBUG
// Add CORS
builder.Services.AddCors(options =>
{
@ -117,7 +121,7 @@ builder.Services.AddCors(options =>
.AllowAnyMethod()
.AllowAnyHeader();
});
options.AddPolicy("AllowAdmin", policy =>
{
policy.WithOrigins("http://localhost:3000", "http://127.0.0.1:3000")
@ -126,31 +130,30 @@ builder.Services.AddCors(options =>
.AllowCredentials();
});
});
#endif
var app = builder.Build();
// 全局异常处理中间件(放在最前面)
app.UseGlobalExceptionHandler();
#if DEBUG
// Enable CORS
app.UseCors("AllowAdmin");
#endif
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
app.UseSwagger();
app.UseSwaggerUI(c =>
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "相宜相亲 后台管理API v1");
c.RoutePrefix = "swagger";
c.DocumentTitle = "相宜相亲 后台管理API文档";
c.DefaultModelsExpandDepth(2);
c.DefaultModelRendering(Swashbuckle.AspNetCore.SwaggerUI.ModelRendering.Model);
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
c.EnableDeepLinking();
c.DisplayRequestDuration();
});
}
c.SwaggerEndpoint("/swagger/v1/swagger.json", "相宜相亲 后台管理API v1");
c.RoutePrefix = "swagger";
c.DocumentTitle = "相宜相亲 后台管理API文档";
c.DefaultModelsExpandDepth(2);
c.DefaultModelRendering(Swashbuckle.AspNetCore.SwaggerUI.ModelRendering.Model);
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
c.EnableDeepLinking();
c.DisplayRequestDuration();
});
app.UseHttpsRedirection();

View File

@ -1,6 +1,9 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models;
using Serilog;
using System.Reflection;
using XiangYi.AppApi.Extensions;
using XiangYi.AppApi.Filters;
using XiangYi.AppApi.Middlewares;
@ -26,9 +29,9 @@ builder.Services.AddControllersWithValidation();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "相宜相亲 小程序API",
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "相宜相亲 小程序API",
Version = "v1",
Description = "相宜相亲微信小程序后端API接口文档",
Contact = new OpenApiContact
@ -37,7 +40,7 @@ builder.Services.AddSwaggerGen(c =>
Email = "tech@xiangyi.com"
}
});
// 添加JWT认证支持到Swagger
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
@ -48,7 +51,7 @@ builder.Services.AddSwaggerGen(c =>
Scheme = "Bearer",
BearerFormat = "JWT"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
@ -106,6 +109,7 @@ builder.Services.AddRepositories();
builder.Services.AddInfrastructureServices(builder.Configuration);
// Add CORS
#if DEBUG
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
@ -115,31 +119,31 @@ builder.Services.AddCors(options =>
.AllowAnyHeader();
});
});
#endif
var app = builder.Build();
// 全局异常处理中间件(放在最前面)
app.UseGlobalExceptionHandler();
#if DEBUG
// Enable CORS
app.UseCors("AllowAll");
#endif
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
app.UseSwagger();
app.UseSwaggerUI(c =>
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "相宜相亲 小程序API v1");
c.RoutePrefix = "swagger";
c.DocumentTitle = "相宜相亲 小程序API文档";
c.DefaultModelsExpandDepth(2);
c.DefaultModelRendering(Swashbuckle.AspNetCore.SwaggerUI.ModelRendering.Model);
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
c.EnableDeepLinking();
c.DisplayRequestDuration();
});
}
c.SwaggerEndpoint("/swagger/v1/swagger.json", "相宜相亲 小程序API v1");
c.RoutePrefix = "swagger";
c.DocumentTitle = "相宜相亲 小程序API文档";
c.DefaultModelsExpandDepth(2);
c.DefaultModelRendering(Swashbuckle.AspNetCore.SwaggerUI.ModelRendering.Model);
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
c.EnableDeepLinking();
c.DisplayRequestDuration();
});
app.UseHttpsRedirection();