forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomSwaggerSetup.cs
More file actions
69 lines (56 loc) · 2.43 KB
/
CustomSwaggerSetup.cs
File metadata and controls
69 lines (56 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
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<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)
{
if (app == null) throw new ArgumentNullException(nameof(app));
var apis = new List<string> { "blog-svc" };
app.UseMvc().UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Blog.Core.Gateway-v1");
apis.ForEach(m =>
{
options.SwaggerEndpoint($"/swagger/apiswg/{m}/swagger.json", m);
options.IndexStream = () => app.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Blog.Core.ApiGateway.index.html");
});
options.RoutePrefix = "";
});
}
}
}