82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using DouyinApi.Common;
|
||
using DouyinApi.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 DouyinApi.Extensions.CustomApiVersion;
|
||
namespace DouyinApi.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, "DouyinApi.Gateway.xml");
|
||
c.IncludeXmlComments(xmlPath, true);
|
||
|
||
c.OperationFilter<AddResponseHeadersFilter>();
|
||
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
|
||
|
||
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
||
|
||
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<Stream> streamHtml)
|
||
{
|
||
if (app == null) throw new ArgumentNullException(nameof(app));
|
||
|
||
var apis = new List<string> { "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 = "";
|
||
});
|
||
}
|
||
|
||
|
||
}
|
||
}
|