使用 Open API 和 Scalar UI

This commit is contained in:
milimoe 2025-01-19 01:34:20 +08:00
parent f10c24f978
commit 8dbcb61464
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
3 changed files with 10 additions and 36 deletions

View File

@ -25,7 +25,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
<PackageReference Include="Scalar.AspNetCore" Version="2.0.1" />
<PackageReference Include="System.Text.Json" Version="9.0.0" /> <PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Console;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Common.Addon; using Milimoe.FunGame.Core.Library.Common.Addon;
using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Common.Network;
@ -21,6 +20,7 @@ using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Utility; using Milimoe.FunGame.Server.Utility;
using Milimoe.FunGame.WebAPI.Architecture; using Milimoe.FunGame.WebAPI.Architecture;
using Milimoe.FunGame.WebAPI.Services; using Milimoe.FunGame.WebAPI.Services;
using Scalar.AspNetCore;
WebAPIListener listener = new(); WebAPIListener listener = new();
@ -115,40 +115,13 @@ try
}); });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options => builder.Services.AddOpenApi();
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "ÊäÈë Auth ·µ»ØµÄ BearerToken",
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
// Ìí¼Ó CORS ·þÎñ // Ìí¼Ó CORS ·þÎñ
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowSpecificOrigin", policy => options.AddPolicy("AllowSpecificOrigin", policy =>
{ {
policy.AllowAnyOrigin() policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
.AllowAnyHeader()
.AllowAnyMethod();
}); });
}); });
// Ìí¼Ó JWT ÈÏÖ¤ // Ìí¼Ó JWT ÈÏÖ¤
@ -185,8 +158,8 @@ try
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.MapOpenApi();
app.UseSwaggerUI(); app.MapScalarApiReference();
} }
app.UseDefaultFiles(); app.UseDefaultFiles();

View File

@ -13,7 +13,7 @@
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "scalar",
"applicationUrl": "http://localhost:5117", "applicationUrl": "http://localhost:5117",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
@ -23,7 +23,7 @@
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "scalar",
"applicationUrl": "https://localhost:7162;http://localhost:5117", "applicationUrl": "https://localhost:7162;http://localhost:5117",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
@ -32,7 +32,7 @@
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "scalar",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }