Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d0fe883e74 | ||
|
|
e4773f3154 | ||
|
|
ba7b3ecd60 | ||
|
|
f52c19938f | ||
|
|
83ab8ba385 | ||
|
|
7d70e8326a |
@@ -1,4 +1,4 @@
|
|||||||
name: CI
|
name: PublishNugetPackage
|
||||||
|
|
||||||
on: [create]
|
on: [create]
|
||||||
|
|
||||||
@@ -29,6 +29,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
version=`git describe --tags`
|
version=`git describe --tags`
|
||||||
dotnet pack --configuration release -p:PackageVersion=$version
|
dotnet pack --configuration release -p:PackageVersion=$version
|
||||||
dotnet nuget push bin/release/simcu.simapi.$version.nupkg -k ${NUGET_APIKEY} -s https://www.nuget.org/api/v2/package
|
dotnet nuget push bin/release/Simcu.SimApi.$version.nupkg -k ${NUGET_APIKEY} -s https://www.nuget.org/api/v2/package
|
||||||
env:
|
env:
|
||||||
APIKEY: ${{ secrets.APPKEY }}
|
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ namespace SimApi.Controllers
|
|||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected static void Error(int code = 500, string message = "")
|
protected static void Error(int code = 500, string message = "服务器错误")
|
||||||
{
|
{
|
||||||
throw new SimApiException(code, message);
|
throw new SimApiException(code, message);
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ namespace SimApi.Controllers
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhen(bool condition, int code = 500, string message = "")
|
protected static void ErrorWhen(bool condition, int code = 500, string message = "服务器错误")
|
||||||
{
|
{
|
||||||
if (condition)
|
if (condition)
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,7 @@ namespace SimApi.Controllers
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在")
|
protected static void ErrorWhenNull(object condition, int code = 404, string message = "资源不存在")
|
||||||
{
|
{
|
||||||
ErrorWhen(condition == null, code, message);
|
ErrorWhen(condition == null, code, message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,9 +54,12 @@ namespace SimApi.Helpers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="uuid">登陆标识</param>
|
/// <param name="uuid">登陆标识</param>
|
||||||
public void Logout(string uuid)
|
public void Logout(string uuid)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(uuid))
|
||||||
{
|
{
|
||||||
Cache.Remove(uuid);
|
Cache.Remove(uuid);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace SimApiApi.Helpers
|
namespace SimApi.Helpers
|
||||||
{
|
{
|
||||||
public static class SimApiUtil
|
public static class SimApiUtil
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,14 +23,26 @@ namespace SimApi.Middlewares
|
|||||||
|
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
|
if (context.Request.Headers.ContainsKey("Query-Id"))
|
||||||
|
{
|
||||||
|
context.Response.Headers["Query-Id"] = context.Request.Headers["Query-Id"];
|
||||||
|
}
|
||||||
|
|
||||||
var response = new SimApiBaseResponse();
|
var response = new SimApiBaseResponse();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Next(context);
|
await Next(context);
|
||||||
}
|
}
|
||||||
catch (SimApiException ex)
|
catch (SimApiException ex)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(ex.Message))
|
||||||
|
{
|
||||||
|
response.SetCode(ex.Code);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
response.SetCodeMsg(ex.Code, ex.Message);
|
response.SetCodeMsg(ex.Code, ex.Message);
|
||||||
|
}
|
||||||
ErrorResponse(context, response);
|
ErrorResponse(context, response);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
+3
-1
@@ -7,10 +7,12 @@
|
|||||||
<Version>0.2.3</Version>
|
<Version>0.2.3</Version>
|
||||||
<Authors>xRain@SimcuTeam</Authors>
|
<Authors>xRain@SimcuTeam</Authors>
|
||||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||||
<PackageId>simcu.simapi</PackageId>
|
<PackageId>Simcu.SimApi</PackageId>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
|
<ReleaseVersion>1.1.1</ReleaseVersion>
|
||||||
|
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
+16
-13
@@ -13,32 +13,35 @@ namespace SimApi
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Extensions
|
public static class Extensions
|
||||||
{
|
{
|
||||||
|
public static string DocumentTitle = "";
|
||||||
|
public static string DocumentDescription = "";
|
||||||
|
|
||||||
//**********快捷添加**************
|
//**********快捷添加**************
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加整个SimApiAPI,同时增加CORS规则
|
/// 添加整个SimAPI,同时增加CORS规则
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
/// <param name="title"></param>
|
/// <param name="title"></param>
|
||||||
/// <param name="description"></param>
|
/// <param name="description"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddSimApiApi(this IServiceCollection builder, string title,
|
public static IServiceCollection AddSimApi(this IServiceCollection builder, string title,
|
||||||
string description = null)
|
string description = null)
|
||||||
{
|
{
|
||||||
return builder.AddSimApiAuth().AddSimApiDoc(title, description).AddCors().AddSimApiUpload();
|
return builder.AddSimApiAuth().AddSimApiDoc(title, description).AddCors().AddSimApiUpload();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 使用所有SimApiApi自定义中间件
|
/// 使用所有SimApi自定义中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
/// <param name="title"></param>
|
/// <param name="title"></param>
|
||||||
/// <param name="staticFileroot"></param>
|
/// <param name="staticFileroot"></param>
|
||||||
/// <param name="submitMethods"></param>
|
/// <param name="submitMethods"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseSimApiApi(this IApplicationBuilder builder, string title = "API文档", params SubmitMethod[] submitMethods)
|
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
||||||
{
|
{
|
||||||
return builder.UseSimApiException().UseSimApiDoc(title, submitMethods).UseMiddleware<SimApiAuthMiddleware>().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()).UseSimApiUpload();
|
return builder.UseSimApiException().UseSimApiDoc(submitMethods).UseMiddleware<SimApiAuthMiddleware>().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()).UseSimApiUpload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -75,9 +78,11 @@ namespace SimApi
|
|||||||
public static IServiceCollection AddSimApiDoc(this IServiceCollection builder, string title,
|
public static IServiceCollection AddSimApiDoc(this IServiceCollection builder, string title,
|
||||||
string description = null)
|
string description = null)
|
||||||
{
|
{
|
||||||
|
DocumentTitle = title;
|
||||||
|
DocumentDescription = description;
|
||||||
return builder.AddSwaggerGen(x =>
|
return builder.AddSwaggerGen(x =>
|
||||||
{
|
{
|
||||||
x.SwaggerDoc("api", new OpenApiInfo { Title = title, Description = description });
|
x.SwaggerDoc("api", new OpenApiInfo { Title = DocumentTitle, Description = DocumentDescription });
|
||||||
x.EnableAnnotations();
|
x.EnableAnnotations();
|
||||||
x.AddSecurityRequirement(new OpenApiSecurityRequirement
|
x.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
{
|
{
|
||||||
@@ -126,14 +131,13 @@ namespace SimApi
|
|||||||
/// <param name="title">文档标题</param>
|
/// <param name="title">文档标题</param>
|
||||||
/// <param name="submitMethods">文档支持的提交方式(如果不指定,默认使用POST)</param>
|
/// <param name="submitMethods">文档支持的提交方式(如果不指定,默认使用POST)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseSimApiDoc(this IApplicationBuilder builder, string title,
|
public static IApplicationBuilder UseSimApiDoc(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
||||||
params SubmitMethod[] submitMethods)
|
|
||||||
{
|
{
|
||||||
return builder.UseSwagger(x => x.RouteTemplate = "docs/{documentName}.json").UseSwaggerUI(x =>
|
return builder.UseSwagger(x => x.RouteTemplate = "docs/{documentName}.json").UseSwaggerUI(x =>
|
||||||
{
|
{
|
||||||
x.RoutePrefix = "docs";
|
x.RoutePrefix = "docs";
|
||||||
x.DocumentTitle = title;
|
x.DocumentTitle = DocumentTitle;
|
||||||
x.SwaggerEndpoint("/docs/api.json", name: title);
|
x.SwaggerEndpoint("/docs/api.json", name: DocumentTitle);
|
||||||
x.EnableValidator();
|
x.EnableValidator();
|
||||||
if (submitMethods.Length > 0)
|
if (submitMethods.Length > 0)
|
||||||
{
|
{
|
||||||
@@ -156,10 +160,9 @@ namespace SimApi
|
|||||||
/// <param name="title">文档标题</param>
|
/// <param name="title">文档标题</param>
|
||||||
/// <param name="submitMethods">API提交方式定义</param>
|
/// <param name="submitMethods">API提交方式定义</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseSimApiDocEx(this IApplicationBuilder builder, string title = "API文档",
|
public static IApplicationBuilder UseSimApiDocEx(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
|
||||||
params SubmitMethod[] submitMethods)
|
|
||||||
{
|
{
|
||||||
return builder.UseSimApiException().UseSimApiDoc(title, submitMethods);
|
return builder.UseSimApiException().UseSimApiDoc(submitMethods);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user