using Meowv.Blog.Configurations;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Meowv.Blog.Swagger
{
public static class MeowvBlogSwaggerExtensions
{
public static IServiceCollection AddSwagger(this IServiceCollection services)
{
return services.AddSwaggerGen(options =>
{
//options.SwaggerDoc(“v1”, new OpenApiInfo
//{
// Version = “1.0.0”,
// Title = “我的接口啊”,
// Description = “接口描述”
//});
ApiInfos.ForEach(x=>
{
options.SwaggerDoc(x.UrlPrefix,x.OpenApiInfo);
});
//options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, “Meowv.Blog.HttpApi.xml”));
//options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, “Meowv.Blog.Domain.xml”));
//options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, “Meowv.Blog.Application.Contracts.xml”));
});
}
public static void UseSwaggerUI(this IApplicationBuilder app)
{
//app.UseSwaggerUI(options =>
//{
// options.SwaggerEndpoint($"/swagger/v1/swagger.json", "默认接口");
//});
//遍历分组信息,生成Json
app.UseSwaggerUI(options =>
{
ApiInfos.ForEach(x =>
{
options.SwaggerEndpoint($"/swagger/{x.UrlPrefix}/swagger.json", x.Name);
});
//模型的默认扩展深度,设置为-1完全隐藏类型
options.DefaultModelExpandDepth(-1);
//API文档仅展开标记
options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
//API前缀设置为空
options.RoutePrefix = string.Empty;
//API页面Title
options.DocumentTitle = "小学生文档接口";
});
}
static string version = AppSettings.ApiVersion;
static string description = "Blog:***Github***HangFire任务调度中心";
/// <summary>
/// Swagger分组信息,将进行遍历使用
/// </summary>
private static readonly List<SwaggerApiInfo> ApiInfos = new List<SwaggerApiInfo>()
{
new SwaggerApiInfo
{
UrlPrefix = Grouping.GroupName_v1,
Name = "博客前台接口",
OpenApiInfo = new OpenApiInfo
{
Version = version,
Title = "小学生 - 博客前台接口",
Description = description
}
},
new SwaggerApiInfo
{
UrlPrefix = Grouping.GroupName_v2,
Name = "博客后台接口",
OpenApiInfo = new OpenApiInfo
{
Version = version,
Title = "小学生 - 博客后台接口",
Description = description
}
},
new SwaggerApiInfo
{
UrlPrefix = Grouping.GroupName_v3,
Name = "通用公共接口",
OpenApiInfo = new OpenApiInfo
{
Version = version,
Title = "小学生 - 通用公共接口",
Description = description
}
},
new SwaggerApiInfo
{
UrlPrefix = Grouping.GroupName_v4,
Name = "JWT授权接口",
OpenApiInfo = new OpenApiInfo
{
Version = version,
Title = "小学生 - JWT授权接口",
Description = description
}
}
};
}
internal class SwaggerApiInfo
{
/// <summary>
/// URL前缀
/// </summary>
public string UrlPrefix { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// <see cref="Microsoft.OpenApi.Models.OpenApiInfo"/>
/// </summary>
public OpenApiInfo OpenApiInfo { get; set; }
}
}