Compare commits

..
17 Commits
Author SHA1 Message Date
xrain 8f41ba0150 move common controller to api group 2021-05-30 03:35:27 +08:00
xrain 9bce77a08e fix 301 302 2021-05-29 14:55:14 +08:00
xrain 8c7f4ac12f add 3.1 support 2021-05-29 11:41:03 +08:00
xrain 856c70d4ac exceptionMiddleware now capture statusCode != 200 response 2021-05-29 11:33:30 +08:00
xrain dfa9fbf909 fix controller Error return wrong message 2020-12-29 17:39:06 +08:00
xrain 883315bc30 add forwardheaders 2020-12-29 17:18:02 +08:00
xrain 4ed517ac90 fix ci 2020-11-28 18:15:13 +08:00
xrain eacbbc3071 fix ci 2020-11-28 18:13:19 +08:00
xrain ee3a3a9d56 update build to .net5 2020-11-28 18:09:45 +08:00
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
xrain f52c19938f fix ci 2020-08-05 15:44:58 +08:00
xrain 83ab8ba385 fix ci 2020-08-05 15:36:39 +08:00
12 changed files with 87 additions and 44 deletions
+4 -17
View File
@@ -3,20 +3,7 @@ name: PublishNugetPackage
on: [create] on: [create]
jobs: jobs:
build:
name: Build Project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.101'
- name: Build
run: dotnet build
publish: publish:
needs: build
name: Publish Project to Nuget name: Publish Project to Nuget
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@@ -24,11 +11,11 @@ jobs:
- name: Setup .NET Core - name: Setup .NET Core
uses: actions/setup-dotnet@v1 uses: actions/setup-dotnet@v1
with: with:
dotnet-version: '3.1.101' dotnet-version: '5.0.100'
- name: Publish - name: Publish
run: | run: |
version=`git describe --tags` version=`git describe --tags`
dotnet pack --configuration release -p:PackageVersion=$version dotnet build --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 }}
+9
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Exceptions; using SimApi.Exceptions;
@@ -14,6 +15,7 @@ namespace SimApi.Attributes
{ {
private string[] Types { get; } private string[] Types { get; }
//默认是user登录类型 //默认是user登录类型
public SimApiAuthAttribute() public SimApiAuthAttribute()
{ {
@@ -32,6 +34,13 @@ namespace SimApi.Attributes
Types = types; Types = types;
} }
//只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url)
{
Types = new[] { type };
new HttpPostAttribute(url);
}
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"]; var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"];
+3
View File
@@ -110,10 +110,13 @@ namespace SimApi.Communications
/// 动态内容列表 /// 动态内容列表
/// </summary> /// </summary>
public T List { get; set; } public T List { get; set; }
//当前页码 //当前页码
public int Page { get; set; } public int Page { get; set; }
//每页条数 //每页条数
public int Count { get; set; } public int Count { get; set; }
//总计条数 //总计条数
public int Total { get; set; } public int Total { get; set; }
} }
+1 -1
View File
@@ -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);
} }
+2
View File
@@ -6,6 +6,7 @@ using SimApi.Helpers;
namespace SimApi.Controllers namespace SimApi.Controllers
{ {
[ApiExplorerSettings(GroupName = "api")]
public class YYCommonController : SimApiBaseController public class YYCommonController : SimApiBaseController
{ {
private SimApiAuth Auth { get; } private SimApiAuth Auth { get; }
@@ -53,6 +54,7 @@ namespace SimApi.Controllers
{ {
token = Request.Headers["Token"]; token = Request.Headers["Token"];
} }
Auth.Logout(token); Auth.Logout(token);
return new SimApiBaseResponse(); return new SimApiBaseResponse();
} }
+3
View File
@@ -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);
} }
}
} }
} }
+1 -1
View File
@@ -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
{ {
+21
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using SimApi.Communications; using SimApi.Communications;
@@ -23,14 +24,34 @@ 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);
if (context.Response.StatusCode != 200)
{
if (!new[] {301, 302}.Contains(context.Response.StatusCode))
{
throw new SimApiException(context.Response.StatusCode);
}
}
} }
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)
+1 -1
View File
@@ -1,4 +1,4 @@
# YYApi 基于Asp.net Core 3的一个基础辅助包 # YYApi 基于Asp.net Core 3/5的一个基础辅助包
![CI](https://github.com/YY-Tech/YYApi/workflows/CI/badge.svg) ![CI](https://github.com/YY-Tech/YYApi/workflows/CI/badge.svg)
+9 -4
View File
@@ -1,18 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<PackOnBuild>true</PackOnBuild> <PackOnBuild>true</PackOnBuild>
<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>5.0.0</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
<ItemGroup> <ItemGroup>
<Folder Include="Helpers\" /> <Folder Include="Helpers\" />
<Folder Include="Communications\" /> <Folder Include="Communications\" />
@@ -22,8 +27,8 @@
<Folder Include="Exceptions\" /> <Folder Include="Exceptions\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.1" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
</ItemGroup> </ItemGroup>
<ProjectExtensions> <ProjectExtensions>
<MonoDevelop> <MonoDevelop>
+27 -14
View File
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerUI; using Swashbuckle.AspNetCore.SwaggerUI;
using SimApi.Middlewares; using SimApi.Middlewares;
using Microsoft.AspNetCore.HttpOverrides;
namespace SimApi namespace SimApi
{ {
@@ -13,32 +14,44 @@ 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().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();
}); ;
} }
/// <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().UseForwardedHeaders();
} }
@@ -75,9 +88,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 +141,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 +170,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>