2019-12-23 16:32:06 +08:00
|
|
|
|
using System;
|
2020-08-05 15:29:56 +08:00
|
|
|
|
using SimApi.Helpers;
|
2019-12-23 16:32:06 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
2020-08-05 15:29:56 +08:00
|
|
|
|
using SimApi.Middlewares;
|
2020-12-29 17:18:02 +08:00
|
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2019-12-23 16:32:06 +08:00
|
|
|
|
|
2020-08-05 15:29:56 +08:00
|
|
|
|
namespace SimApi
|
2019-12-23 16:32:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加入系统的扩展信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class Extensions
|
|
|
|
|
|
{
|
2020-08-23 21:42:41 +08:00
|
|
|
|
public static string DocumentTitle = "";
|
|
|
|
|
|
public static string DocumentDescription = "";
|
|
|
|
|
|
|
2020-02-04 16:05:09 +08:00
|
|
|
|
//**********快捷添加**************
|
2019-12-23 16:32:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-23 19:51:37 +08:00
|
|
|
|
/// 添加整个SimAPI,同时增加CORS规则
|
2019-12-23 16:32:06 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="title"></param>
|
|
|
|
|
|
/// <param name="description"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-23 19:51:37 +08:00
|
|
|
|
public static IServiceCollection AddSimApi(this IServiceCollection builder, string title,
|
2019-12-23 16:32:06 +08:00
|
|
|
|
string description = null)
|
|
|
|
|
|
{
|
2020-12-29 17:18:02 +08:00
|
|
|
|
return builder.AddSimApiAuth().AddSimApiDoc(title, description).AddCors().AddSimApiUpload().Configure<ForwardedHeadersOptions>(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
|
|
|
|
|
|
ForwardedHeaders.XForwardedProto;
|
|
|
|
|
|
// Only loopback proxies are allowed by default.
|
|
|
|
|
|
// Clear that restriction because forwarders are enabled by explicit
|
|
|
|
|
|
// configuration.
|
|
|
|
|
|
options.KnownNetworks.Clear();
|
|
|
|
|
|
options.KnownProxies.Clear();
|
|
|
|
|
|
}); ;
|
2020-02-04 16:05:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-23 19:51:37 +08:00
|
|
|
|
/// 使用所有SimApi自定义中间件
|
2020-02-04 16:05:09 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="title"></param>
|
2020-02-18 06:37:48 +08:00
|
|
|
|
/// <param name="staticFileroot"></param>
|
2020-02-04 16:05:09 +08:00
|
|
|
|
/// <param name="submitMethods"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-23 21:42:41 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
2020-02-04 16:05:09 +08:00
|
|
|
|
{
|
2020-12-29 17:18:02 +08:00
|
|
|
|
return builder.UseSimApiException().UseSimApiDoc(submitMethods).UseMiddleware<SimApiAuthMiddleware>().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()).UseSimApiUpload().UseForwardedHeaders();
|
2020-02-04 16:05:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//*********组件快捷方式*************
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加自定义授权认证中间价
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IServiceCollection AddSimApiAuth(this IServiceCollection builder)
|
2020-02-04 16:05:09 +08:00
|
|
|
|
{
|
2020-08-05 15:29:56 +08:00
|
|
|
|
return builder.AddScoped<SimApiAuth>();
|
2019-12-23 16:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-18 06:37:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加Base64上传组件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IServiceCollection AddSimApiUpload(this IServiceCollection builder)
|
2020-02-18 06:37:48 +08:00
|
|
|
|
{
|
2020-08-05 15:29:56 +08:00
|
|
|
|
return builder.AddSingleton<SimApiUpload>();
|
2020-02-18 06:37:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-23 16:32:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加Api文档服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="title">文档标题</param>
|
|
|
|
|
|
/// <param name="description">文档描述</param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IServiceCollection AddSimApiDoc(this IServiceCollection builder, string title,
|
2019-12-23 16:32:06 +08:00
|
|
|
|
string description = null)
|
|
|
|
|
|
{
|
2020-08-23 21:42:41 +08:00
|
|
|
|
DocumentTitle = title;
|
|
|
|
|
|
DocumentDescription = description;
|
2019-12-23 16:32:06 +08:00
|
|
|
|
return builder.AddSwaggerGen(x =>
|
|
|
|
|
|
{
|
2020-08-23 21:42:41 +08:00
|
|
|
|
x.SwaggerDoc("api", new OpenApiInfo { Title = DocumentTitle, Description = DocumentDescription });
|
2019-12-23 16:32:06 +08:00
|
|
|
|
x.EnableAnnotations();
|
|
|
|
|
|
x.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
|
{
|
|
|
|
|
|
{
|
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
|
{
|
|
|
|
|
|
Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "HeaderToken"}
|
|
|
|
|
|
},
|
|
|
|
|
|
new[] {"readAccess", "writeAccess"}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
x.AddSecurityDefinition("HeaderToken",
|
2020-01-17 12:40:22 +08:00
|
|
|
|
new OpenApiSecurityScheme { Name = "Token", In = ParameterLocation.Header });
|
2019-12-23 16:32:06 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-17 12:40:22 +08:00
|
|
|
|
|
2019-12-23 16:32:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 使用异常中间价扩展
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApiException(this IApplicationBuilder builder)
|
2019-12-23 16:32:06 +08:00
|
|
|
|
{
|
2020-08-05 15:29:56 +08:00
|
|
|
|
return builder.UseMiddleware<SimApiExceptionMiddleware>();
|
2019-12-23 16:32:06 +08:00
|
|
|
|
}
|
2020-02-18 06:37:48 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 配置上传组件必须
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApiUpload(this IApplicationBuilder builder)
|
2020-02-18 06:37:48 +08:00
|
|
|
|
{
|
2020-03-11 23:27:17 +08:00
|
|
|
|
return builder.UseStaticFiles(new StaticFileOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
DefaultContentType = "application/x-msdownload",
|
|
|
|
|
|
ServeUnknownFileTypes = true
|
|
|
|
|
|
});
|
2020-02-18 06:37:48 +08:00
|
|
|
|
}
|
2019-12-23 16:32:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 使用文档UI
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="title">文档标题</param>
|
|
|
|
|
|
/// <param name="submitMethods">文档支持的提交方式(如果不指定,默认使用POST)</param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-23 21:42:41 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApiDoc(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
2019-12-23 16:32:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
return builder.UseSwagger(x => x.RouteTemplate = "docs/{documentName}.json").UseSwaggerUI(x =>
|
|
|
|
|
|
{
|
|
|
|
|
|
x.RoutePrefix = "docs";
|
2020-08-23 21:42:41 +08:00
|
|
|
|
x.DocumentTitle = DocumentTitle;
|
|
|
|
|
|
x.SwaggerEndpoint("/docs/api.json", name: DocumentTitle);
|
2019-12-23 16:32:06 +08:00
|
|
|
|
x.EnableValidator();
|
|
|
|
|
|
if (submitMethods.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
x.SupportedSubmitMethods(submitMethods);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
x.SupportedSubmitMethods(SubmitMethod.Post);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
x.DisplayRequestDuration();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 同时使用Api文档和异常处理中间件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <param name="title">文档标题</param>
|
|
|
|
|
|
/// <param name="submitMethods">API提交方式定义</param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-23 21:42:41 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApiDocEx(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
2019-12-23 16:32:06 +08:00
|
|
|
|
{
|
2020-08-23 21:42:41 +08:00
|
|
|
|
return builder.UseSimApiException().UseSimApiDoc(submitMethods);
|
2019-12-23 16:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 使用自定义认证中间件(依赖IDistributedCache)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="builder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-08-05 15:29:56 +08:00
|
|
|
|
public static IApplicationBuilder UseSimApiAuth(this IApplicationBuilder builder)
|
2019-12-23 16:32:06 +08:00
|
|
|
|
{
|
2020-08-05 15:29:56 +08:00
|
|
|
|
return builder.UseMiddleware<SimApiAuthMiddleware>();
|
2019-12-23 16:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-04 16:05:09 +08:00
|
|
|
|
|
2019-12-23 16:32:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|