Compare commits

...
6 Commits
Author SHA1 Message Date
xrain 61672c5770 update build to .net5 2020-11-28 18:05:49 +08:00
xrain c6b9746603 .netcoreapp 5 2020-11-28 18:03:20 +08:00
xrain f7e1b9be2a complete with .net core 5.0.0 2020-11-11 03:28:09 +08:00
xrain d0fe883e74 opt 2020-08-29 11:49:44 +08:00
xrain e4773f3154 fix error response message, error namespace. now useMiddleware dont need api doc title 2020-08-23 21:42:41 +08:00
xrain ba7b3ecd60 fix error namesapce 2020-08-23 19:51:37 +08:00
9 changed files with 57 additions and 25 deletions
+2 -2
View File
@@ -11,7 +11,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.101'
dotnet-version: '5.0.100'
- name: Build
run: dotnet build
@@ -24,7 +24,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.101'
dotnet-version: '5.0.100'
- name: Publish
run: |
version=`git describe --tags`
+9
View File
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SimApi.Communications;
using SimApi.Exceptions;
@@ -14,6 +15,7 @@ namespace SimApi.Attributes
{
private string[] Types { get; }
//默认是user登录类型
public SimApiAuthAttribute()
{
@@ -32,6 +34,13 @@ namespace SimApi.Attributes
Types = types;
}
//只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url)
{
Types = new[] { type };
new HttpPostAttribute(url);
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"];
+3 -3
View File
@@ -47,7 +47,7 @@ namespace SimApi.Controllers
/// <param name="code">错误代码</param>
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
/// <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);
}
@@ -58,7 +58,7 @@ namespace SimApi.Controllers
/// <param name="condition">检测条件</param>
/// <param name="code">错误代码</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)
{
@@ -72,7 +72,7 @@ namespace SimApi.Controllers
/// <param name="condition">检测条件</param>
/// <param name="code">错误代码</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);
}
+4 -1
View File
@@ -55,7 +55,10 @@ namespace SimApi.Helpers
/// <param name="uuid">登陆标识</param>
public void Logout(string uuid)
{
Cache.Remove(uuid);
if (!string.IsNullOrEmpty(uuid))
{
Cache.Remove(uuid);
}
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace SimApiApi.Helpers
namespace SimApi.Helpers
{
public static class SimApiUtil
{
+1 -1
View File
@@ -34,7 +34,7 @@ namespace SimApi.Middlewares
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<SimApiLoginItem>(login));
}
}
return Next(httpContext);
}
}
+13 -1
View File
@@ -23,6 +23,11 @@ namespace SimApi.Middlewares
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();
try
{
@@ -30,7 +35,14 @@ namespace SimApi.Middlewares
}
catch (SimApiException ex)
{
response.SetCodeMsg(ex.Code, ex.Message);
if (string.IsNullOrEmpty(ex.Message))
{
response.SetCode(ex.Code);
}
else
{
response.SetCodeMsg(ex.Code, ex.Message);
}
ErrorResponse(context, response);
}
catch (Exception ex)
+8 -3
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp5.0</TargetFramework>
<OutputType>Library</OutputType>
<PackOnBuild>true</PackOnBuild>
<Version>0.2.3</Version>
@@ -11,8 +11,13 @@
<IsPackable>true</IsPackable>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<ReleaseVersion>5.0.0</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
<ItemGroup>
<Folder Include="Helpers\" />
<Folder Include="Communications\" />
@@ -22,8 +27,8 @@
<Folder Include="Exceptions\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
+16 -13
View File
@@ -13,32 +13,35 @@ namespace SimApi
/// </summary>
public static class Extensions
{
public static string DocumentTitle = "";
public static string DocumentDescription = "";
//**********快捷添加**************
/// <summary>
/// 添加整个SimApiAPI,同时增加CORS规则
/// 添加整个SimAPI,同时增加CORS规则
/// </summary>
/// <param name="builder"></param>
/// <param name="title"></param>
/// <param name="description"></param>
/// <returns></returns>
public static IServiceCollection AddSimApiApi(this IServiceCollection builder, string title,
public static IServiceCollection AddSimApi(this IServiceCollection builder, string title,
string description = null)
{
return builder.AddSimApiAuth().AddSimApiDoc(title, description).AddCors().AddSimApiUpload();
}
/// <summary>
/// 使用所有SimApiApi自定义中间件
/// 使用所有SimApi自定义中间件
/// </summary>
/// <param name="builder"></param>
/// <param name="title"></param>
/// <param name="staticFileroot"></param>
/// <param name="submitMethods"></param>
/// <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,
string description = null)
{
DocumentTitle = title;
DocumentDescription = description;
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.AddSecurityRequirement(new OpenApiSecurityRequirement
{
@@ -126,14 +131,13 @@ namespace SimApi
/// <param name="title">文档标题</param>
/// <param name="submitMethods">文档支持的提交方式(如果不指定,默认使用POST)</param>
/// <returns></returns>
public static IApplicationBuilder UseSimApiDoc(this IApplicationBuilder builder, string title,
params SubmitMethod[] submitMethods)
public static IApplicationBuilder UseSimApiDoc(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
{
return builder.UseSwagger(x => x.RouteTemplate = "docs/{documentName}.json").UseSwaggerUI(x =>
{
x.RoutePrefix = "docs";
x.DocumentTitle = title;
x.SwaggerEndpoint("/docs/api.json", name: title);
x.DocumentTitle = DocumentTitle;
x.SwaggerEndpoint("/docs/api.json", name: DocumentTitle);
x.EnableValidator();
if (submitMethods.Length > 0)
{
@@ -156,10 +160,9 @@ namespace SimApi
/// <param name="title">文档标题</param>
/// <param name="submitMethods">API提交方式定义</param>
/// <returns></returns>
public static IApplicationBuilder UseSimApiDocEx(this IApplicationBuilder builder, string title = "API文档",
params SubmitMethod[] submitMethods)
public static IApplicationBuilder UseSimApiDocEx(this IApplicationBuilder builder, params SubmitMethod[] submitMethods)
{
return builder.UseSimApiException().UseSimApiDoc(title, submitMethods);
return builder.UseSimApiException().UseSimApiDoc(submitMethods);
}
/// <summary>