using Blahblah.FlowerStory.Server.Data; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; namespace Blahblah.FlowerStory.Server; /// public class Program { /// public const string ProjectName = "Flower Story"; /// public const string Version = "0.23.523"; /// public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { //options.OperationFilter(); var scheme = new OpenApiSecurityScheme { Description = "授权头。 示例: \"RG//HkvcTZdBospBOT6OuoWfsc1GS+P/js9zFdflBr0=\"", Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Authorization" }, Scheme = "oauth2", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }; options.AddSecurityDefinition("Authorization", scheme); options.AddSecurityRequirement(new OpenApiSecurityRequirement { [scheme] = Array.Empty() }); options.SwaggerDoc(Version, new OpenApiInfo { Title = ProjectName, Version = Version, Description = "

花事记录,贴心的帮您记录花园中的点点滴滴。

API 文档

" }); options.IncludeXmlComments(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Server.xml")); }); builder.Services.AddDbContext(options => options.UseSqlite("DataSource=flower.db;Cache=Shared")); builder.Services.AddScoped(); var app = builder.Build(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint($"/swagger/{Version}/swagger.json", ProjectName); }); } app.UseAuthorization(); app.MapControllers(); app.Run(); } } /// public class SwaggerHttpHeaderOperation : IOperationFilter { /// public void Apply(OpenApiOperation operation, OperationFilterContext context) { var required = context.ApiDescription.RelativePath switch { "user/update" or "user/profile" or "user/logout" => true, _ => false }; if (required) { operation.Parameters.Add(new OpenApiParameter { Name = "Authorization", Description = "授权 Token", In = ParameterLocation.Header, Required = true, Schema = new OpenApiSchema { Type = "string" } }); } } }