using Blog.Core.Common; using Blog.Core.Extensions.Middlewares; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; using Swashbuckle.AspNetCore.SwaggerUI; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using static Blog.Core.Extensions.CustomApiVersion; namespace Blog.Core.Gateway.Extensions { public static class CustomSwaggerSetup { public static void AddCustomSwaggerSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); var basePath = AppContext.BaseDirectory; services.AddMvc(option => option.EnableEndpointRouting = false); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "自定义网关 接口文档", }); var xmlPath = Path.Combine(basePath, "Blog.Core.Gateway.xml"); c.IncludeXmlComments(xmlPath, true); c.OperationFilter(); c.OperationFilter(); c.OperationFilter(); c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); }); } public static void UseCustomSwaggerMildd(this IApplicationBuilder app, Func streamHtml) { if (app == null) throw new ArgumentNullException(nameof(app)); var apis = new List { "blog-svc" }; app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint($"/swagger/v1/swagger.json", "gateway"); apis.ForEach(m => { c.SwaggerEndpoint($"/swagger/apiswg/{m}/swagger.json", m); }); if (streamHtml.Invoke() == null) { var msg = "index.html的属性,必须设置为嵌入的资源"; throw new Exception(msg); } c.IndexStream = streamHtml; c.RoutePrefix = ""; }); } } }