Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb291691d5 | ||
|
|
47a48d0103 | ||
|
|
4c50acc309 | ||
|
|
be9092bf3c | ||
|
|
345aaa261e | ||
|
|
f98cc6ff5a |
@@ -14,7 +14,7 @@ jobs:
|
|||||||
- name: Setup .NET Core
|
- name: Setup .NET Core
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v1
|
||||||
with:
|
with:
|
||||||
dotnet-version: "7.0.100"
|
dotnet-version: "8.0.200"
|
||||||
- name: Publish
|
- name: Publish
|
||||||
run: |
|
run: |
|
||||||
version=`git describe --tags`
|
version=`git describe --tags`
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
|||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
namespace SimApi.Attributes
|
namespace SimApi.Attributes;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测登录中间件
|
/// 检测登录中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -19,10 +19,7 @@ namespace SimApi.Attributes
|
|||||||
//默认是user登录类型
|
//默认是user登录类型
|
||||||
public SimApiAuthAttribute()
|
public SimApiAuthAttribute()
|
||||||
{
|
{
|
||||||
Types = new[]
|
Types = new[] { "user" };
|
||||||
{
|
|
||||||
"user"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//只检测一种用户类型的快捷方式
|
//只检测一种用户类型的快捷方式
|
||||||
@@ -40,10 +37,7 @@ namespace SimApi.Attributes
|
|||||||
//只检测一种用户类型的快捷方式
|
//只检测一种用户类型的快捷方式
|
||||||
public SimApiAuthAttribute(string type, string url)
|
public SimApiAuthAttribute(string type, string url)
|
||||||
{
|
{
|
||||||
Types = new[]
|
Types = new[] { type };
|
||||||
{
|
|
||||||
type
|
|
||||||
};
|
|
||||||
new HttpPostAttribute(url);
|
new HttpPostAttribute(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,4 +56,3 @@ namespace SimApi.Attributes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using Swashbuckle.AspNetCore.Annotations;
|
using Swashbuckle.AspNetCore.Annotations;
|
||||||
|
|
||||||
namespace SimApi.Attributes
|
namespace SimApi.Attributes;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 快捷自定义接口文档类
|
/// 快捷自定义接口文档类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -22,4 +22,3 @@ namespace SimApi.Attributes
|
|||||||
// Produces = new[] {"application/json"};
|
// Produces = new[] {"application/json"};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 只有ID的请求
|
/// 只有ID的请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -20,4 +20,3 @@
|
|||||||
/// 基础分页请求
|
/// 基础分页请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SimApiBasePageRequest(int Page, int Count);
|
public record SimApiBasePageRequest(int Page, int Count);
|
||||||
}
|
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 基础相应
|
/// 基础相应
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -14,27 +14,13 @@ namespace SimApi.Communications
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Dictionary<int, string> MsgBox = new()
|
private static readonly Dictionary<int, string> MsgBox = new()
|
||||||
{
|
{
|
||||||
{
|
{ 200, "成功" },
|
||||||
200, "成功"
|
{ 204, "没有数据" },
|
||||||
},
|
{ 400, "参数错误" },
|
||||||
{
|
{ 401, "需要登录" },
|
||||||
204, "没有数据"
|
{ 403, "无权访问" },
|
||||||
},
|
{ 404, "接口不存在" },
|
||||||
{
|
{ 500, "服务器错误" }
|
||||||
400, "参数错误"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
401, "需要登录"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
403, "无权访问"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
404, "接口不存在"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
500, "服务器错误"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
||||||
@@ -63,8 +49,12 @@ namespace SimApi.Communications
|
|||||||
/// 动态内容分页
|
/// 动态内容分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public record SimApiBasePageResponse<T>
|
public record SimApiBasePageResponse<T>(
|
||||||
(T List, int Page = 1, int Count = 1, int Total = 1, int Code = 200,
|
T List,
|
||||||
|
int Page = 1,
|
||||||
|
int Count = 1,
|
||||||
|
int Total = 1,
|
||||||
|
int Code = 200,
|
||||||
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -73,4 +63,3 @@ namespace SimApi.Communications
|
|||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
||||||
Message);
|
Message);
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 登录信息中间件
|
/// 登录信息中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SimApiLoginItem(string Id, string[] Type);
|
public record SimApiLoginItem(string Id, string[] Type);
|
||||||
}
|
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||||
|
|
||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文档组配置
|
/// 文档组配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -31,9 +30,13 @@ namespace SimApi.Configs
|
|||||||
public class SimApiAuthOption
|
public class SimApiAuthOption
|
||||||
{
|
{
|
||||||
public string[] Type { get; set; } = new[] { "SimApiAuth" };
|
public string[] Type { get; set; } = new[] { "SimApiAuth" };
|
||||||
|
|
||||||
public string Description { get; set; } = "认证服务器颁发的AccessToken";
|
public string Description { get; set; } = "认证服务器颁发的AccessToken";
|
||||||
|
|
||||||
public string AuthorizationUrl { get; set; }
|
public string AuthorizationUrl { get; set; }
|
||||||
|
|
||||||
public string TokenUrl { get; set; }
|
public string TokenUrl { get; set; }
|
||||||
|
|
||||||
public Dictionary<string, string> Scopes { get; set; }
|
public Dictionary<string, string> Scopes { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,4 +73,3 @@ namespace SimApi.Configs
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post };
|
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post };
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
{
|
|
||||||
public class SimApiOptions
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -61,14 +61,14 @@ namespace SimApi.Configs
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Swagger文档相关配置,需要启用 EnableSimApiDoc
|
/// Swagger文档相关配置,需要启用 EnableSimApiDoc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
public SimApiDocOptions SimApiDocOptions { get; set; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// S3兼容的存储系统配置,需要启用 EnableSimApiStorage
|
/// S3兼容的存储系统配置,需要启用 EnableSimApiStorage
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new();
|
||||||
|
|
||||||
public SimApiSynapseOptions SimApiSynapseOptions { get; set; } = new SimApiSynapseOptions();
|
public SimApiSynapseOptions SimApiSynapseOptions { get; set; } = new();
|
||||||
|
|
||||||
public void ConfigureSimApiSynapse(Action<SimApiSynapseOptions> options = null)
|
public void ConfigureSimApiSynapse(Action<SimApiSynapseOptions> options = null)
|
||||||
{
|
{
|
||||||
@@ -90,4 +90,3 @@ namespace SimApi.Configs
|
|||||||
options?.Invoke(SimApiStorageOptions);
|
options?.Invoke(SimApiStorageOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
{
|
|
||||||
public class SimApiStorageOptions
|
public class SimApiStorageOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -21,4 +21,3 @@ namespace SimApi.Configs
|
|||||||
|
|
||||||
public string SecretKey { get; set; }
|
public string SecretKey { get; set; }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -4,10 +4,9 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
|||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
using SimApi.Attributes;
|
|
||||||
|
|
||||||
namespace SimApi.Controllers
|
namespace SimApi.Controllers;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 基础控制器,所有控制器均继承本控制器
|
/// 基础控制器,所有控制器均继承本控制器
|
||||||
/// 1. 自动验证请求参数
|
/// 1. 自动验证请求参数
|
||||||
@@ -84,4 +83,3 @@ namespace SimApi.Controllers
|
|||||||
return new SimApiBaseResponse<string>(null);
|
return new SimApiBaseResponse<string>(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,21 +1,13 @@
|
|||||||
using System;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Controllers
|
namespace SimApi.Controllers;
|
||||||
{
|
|
||||||
[ApiExplorerSettings(GroupName = "api")]
|
[ApiExplorerSettings(GroupName = "api")]
|
||||||
public class YYCommonController : SimApiBaseController
|
public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||||
{
|
{
|
||||||
private SimApiAuth Auth { get; }
|
|
||||||
|
|
||||||
public YYCommonController(SimApiAuth auth)
|
|
||||||
{
|
|
||||||
Auth = auth;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 错误回馈页面
|
/// 错误回馈页面
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -53,8 +45,7 @@ namespace SimApi.Controllers
|
|||||||
token = Request.Headers["Token"];
|
token = Request.Headers["Token"];
|
||||||
}
|
}
|
||||||
|
|
||||||
Auth.Logout(token);
|
auth.Logout(token);
|
||||||
return new SimApiBaseResponse();
|
return new SimApiBaseResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,16 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
namespace SimApi.Exceptions
|
|
||||||
{
|
namespace SimApi.Exceptions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Api错误捕获异常
|
/// Api错误捕获异常
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiException : Exception
|
public class SimApiException(int code, string message = "") : Exception(message)
|
||||||
{
|
{
|
||||||
public int Code { get; }
|
public int Code { get; } = code;
|
||||||
|
|
||||||
public SimApiException(int code, string message = "") : base(message)
|
|
||||||
{
|
|
||||||
Code = code;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+6
-18
@@ -1,23 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 认证助手
|
/// 认证助手
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiAuth
|
public class SimApiAuth(IDistributedCache cache)
|
||||||
{
|
{
|
||||||
private IDistributedCache Cache { get; }
|
|
||||||
|
|
||||||
public SimApiAuth(IDistributedCache cache)
|
|
||||||
{
|
|
||||||
Cache = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 产生一个Token记录并返回Token
|
/// 产生一个Token记录并返回Token
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -25,10 +17,7 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Login(string id, string type = "user", string token = null)
|
public string Login(string id, string type = "user", string token = null)
|
||||||
{
|
{
|
||||||
return Login(id, new[]
|
return Login(id, new[] { type }, token);
|
||||||
{
|
|
||||||
type
|
|
||||||
}, token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -41,7 +30,7 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
uuid ??= Guid.NewGuid().ToString();
|
uuid ??= Guid.NewGuid().ToString();
|
||||||
var loginItem = new SimApiLoginItem(id, type);
|
var loginItem = new SimApiLoginItem(id, type);
|
||||||
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +42,7 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(uuid))
|
if (!string.IsNullOrEmpty(uuid))
|
||||||
{
|
{
|
||||||
Cache.Remove(uuid);
|
cache.Remove(uuid);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,10 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Minio;
|
using Minio;
|
||||||
using Minio.Exceptions;
|
|
||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
{
|
|
||||||
public class SimApiStorage
|
public class SimApiStorage
|
||||||
{
|
{
|
||||||
private MinioClient Mc { get; }
|
private MinioClient Mc { get; }
|
||||||
@@ -97,7 +96,6 @@ namespace SimApi.Helpers
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.StartsWith("~") ? url + path.Substring(1, path.Length - 1) : $"{ServeUrl}{path}";
|
return path.StartsWith('~') ? string.Concat(url, path.AsSpan(1, path.Length - 1)) : $"{ServeUrl}{path}";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+21
-20
@@ -3,28 +3,33 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
{
|
|
||||||
public static class SimApiUtil
|
public static class SimApiUtil
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前CST时间
|
/// 当前CST时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static DateTime CstNow
|
public static DateTime CstNow => DateTime.UtcNow.AddHours(8);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// JSON序列化常规选项
|
||||||
|
/// </summary>
|
||||||
|
public static JsonSerializerOptions JsonOption => new()
|
||||||
{
|
{
|
||||||
get => DateTime.UtcNow.AddHours(8);
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||||
}
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前秒级时间戳
|
/// 当前秒级时间戳
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static double TimestampNow
|
public static double TimestampNow => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
||||||
{
|
|
||||||
get => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测手机号是否正确
|
/// 检测手机号是否正确
|
||||||
@@ -45,13 +50,13 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Md5(string source, string mode = "x2")
|
public static string Md5(string source, string mode = "x2")
|
||||||
{
|
{
|
||||||
byte[] sor = Encoding.UTF8.GetBytes(source);
|
var sor = Encoding.UTF8.GetBytes(source);
|
||||||
MD5 md5 = MD5.Create();
|
var md5 = MD5.Create();
|
||||||
byte[] result = md5.ComputeHash(sor);
|
var result = md5.ComputeHash(sor);
|
||||||
StringBuilder strbul = new StringBuilder(40);
|
var strbul = new StringBuilder(40);
|
||||||
for (int i = 0; i < result.Length; i++)
|
foreach (var t in result)
|
||||||
{
|
{
|
||||||
strbul.Append(result[i].ToString(mode));
|
strbul.Append(t.ToString(mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
return strbul.ToString();
|
return strbul.ToString();
|
||||||
@@ -64,10 +69,6 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Json(object obj)
|
public static string Json(object obj)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Serialize(obj, new JsonSerializerOptions
|
return JsonSerializer.Serialize(obj, JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-11
@@ -2,17 +2,10 @@ using System;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Logger
|
namespace SimApi.Logger;
|
||||||
{
|
|
||||||
public class SimApiLogger : ILogger
|
|
||||||
{
|
|
||||||
private string Name { get; }
|
|
||||||
|
|
||||||
public SimApiLogger(string name)
|
public class SimApiLogger(string name) : ILogger
|
||||||
{
|
{
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDisposable BeginScope<TState>(TState state) => default!;
|
public IDisposable BeginScope<TState>(TState state) => default!;
|
||||||
|
|
||||||
public bool IsEnabled(LogLevel logLevel) => true;
|
public bool IsEnabled(LogLevel logLevel) => true;
|
||||||
@@ -30,7 +23,7 @@ namespace SimApi.Logger
|
|||||||
_ => ConsoleColor.White
|
_ => ConsoleColor.White
|
||||||
};
|
};
|
||||||
var message =
|
var message =
|
||||||
$"[ {Name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
$"[ {name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
{
|
{
|
||||||
message += $"{exception}\n";
|
message += $"{exception}\n";
|
||||||
@@ -38,4 +31,3 @@ namespace SimApi.Logger
|
|||||||
Console.WriteLine(message);
|
Console.WriteLine(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace SimApi.Logger
|
namespace SimApi.Logger;
|
||||||
{
|
|
||||||
public class SimApiLoggerProvider : ILoggerProvider
|
public class SimApiLoggerProvider : ILoggerProvider
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers =
|
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers = new();
|
||||||
new ConcurrentDictionary<string, SimApiLogger>();
|
|
||||||
|
|
||||||
|
|
||||||
public ILogger CreateLogger(string categoryName)
|
public ILogger CreateLogger(string categoryName)
|
||||||
@@ -19,4 +18,3 @@ namespace SimApi.Logger
|
|||||||
_loggers.Clear();
|
_loggers.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -4,26 +4,19 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
|
||||||
namespace SimApi.Middlewares
|
namespace SimApi.Middlewares;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 认证信息获取中间件
|
/// 认证信息获取中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiAuthMiddleware
|
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||||
{
|
{
|
||||||
private RequestDelegate Next { get; }
|
|
||||||
|
|
||||||
public SimApiAuthMiddleware(RequestDelegate next)
|
|
||||||
{
|
|
||||||
Next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
||||||
{
|
{
|
||||||
string token = null;
|
string token = null;
|
||||||
if (httpContext.Request.Headers.ContainsKey("Token"))
|
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
||||||
{
|
{
|
||||||
token = httpContext.Request.Headers["Token"];
|
token = header;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(token))
|
if (!string.IsNullOrEmpty(token))
|
||||||
@@ -35,7 +28,6 @@ namespace SimApi.Middlewares
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Next(httpContext);
|
return next(httpContext);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,40 +6,27 @@ using SimApi.Communications;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
namespace SimApi.Middlewares
|
namespace SimApi.Middlewares;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异常处理中间件
|
/// 异常处理中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiExceptionMiddleware
|
public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
||||||
{
|
{
|
||||||
private RequestDelegate Next { get; }
|
|
||||||
|
|
||||||
private ILogger<SimApiExceptionMiddleware> Log { get; }
|
|
||||||
|
|
||||||
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
|
||||||
{
|
|
||||||
Log = log;
|
|
||||||
Next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
if (context.Request.Headers.ContainsKey("Query-Id"))
|
if (context.Request.Headers.TryGetValue("Query-Id", out var header))
|
||||||
{
|
{
|
||||||
context.Response.Headers["Query-Id"] = context.Request.Headers["Query-Id"];
|
context.Response.Headers["Query-Id"] = header;
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = new SimApiBaseResponse();
|
SimApiBaseResponse response;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Next(context);
|
await next(context);
|
||||||
if (context.Response.StatusCode != 200)
|
if (context.Response.StatusCode != 200)
|
||||||
{
|
{
|
||||||
if (!new[]
|
if (!new[] { 301, 302 }.Contains(context.Response.StatusCode))
|
||||||
{
|
|
||||||
301, 302
|
|
||||||
}.Contains(context.Response.StatusCode))
|
|
||||||
{
|
{
|
||||||
throw new SimApiException(context.Response.StatusCode);
|
throw new SimApiException(context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
@@ -55,8 +42,8 @@ namespace SimApi.Middlewares
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.LogError(ex.Message);
|
log.LogError(ex.Message);
|
||||||
Log.LogError(ex.StackTrace);
|
log.LogError(ex.StackTrace);
|
||||||
response = new SimApiBaseResponse(500, ex.Message);
|
response = new SimApiBaseResponse(500, ex.Message);
|
||||||
ErrorResponse(context, response);
|
ErrorResponse(context, response);
|
||||||
}
|
}
|
||||||
@@ -72,9 +59,8 @@ namespace SimApi.Middlewares
|
|||||||
if (!context.Response.HasStarted)
|
if (!context.Response.HasStarted)
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = 200;
|
context.Response.StatusCode = 200;
|
||||||
context.Response.Headers.Add("Content-Type", "application/json");
|
context.Response.Headers.Append("Content-Type", "application/json");
|
||||||
context.Response.WriteAsync(response.ToString());
|
context.Response.WriteAsync(response.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Models
|
namespace SimApi.Models;
|
||||||
{
|
|
||||||
public class SimApiBaseModel
|
public class SimApiBaseModel
|
||||||
{
|
{
|
||||||
protected virtual string[] MapperIgnoreField { get; set; } =
|
[Column(Order = 1)]
|
||||||
{
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
"Id", "CreatedAt", "UpdatedAt"
|
|
||||||
};
|
[Column(Order = 9998)]
|
||||||
|
public DateTime UpdatedAt { get; set; } = SimApiUtil.CstNow;
|
||||||
|
|
||||||
|
[Column(Order = 9999)]
|
||||||
|
public DateTime CreatedAt { get; set; } = SimApiUtil.CstNow;
|
||||||
|
|
||||||
|
protected virtual string[] MapperIgnoreField { get; set; } = { "Id", "CreatedAt", "UpdatedAt" };
|
||||||
|
|
||||||
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
||||||
|
|
||||||
@@ -81,4 +87,3 @@ namespace SimApi.Models
|
|||||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# YYApi 基于Asp.net Core 3/5的一个基础辅助包
|
# SimApi 基于.Net的一个基础辅助包
|
||||||
|
|
||||||
[](https://github.com/simcu/simapi-net/actions/workflows/nuget-publish.yml)
|
[](https://github.com/simcu/simapi-net/actions/workflows/nuget-publish.yml)
|
||||||
### 包含了以下组件:
|
### 包含了以下组件:
|
||||||
@@ -52,3 +52,4 @@ services.AddSimApi(options =>
|
|||||||
options.SimApiStorageOptions = Configuration.GetSection("S3").Get<SimApiStorageOptions>();
|
options.SimApiStorageOptions = Configuration.GetSection("S3").Get<SimApiStorageOptions>();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
基于S3的存储,基于RabbitMq的事件RPC调用,自定义Logger格式,自定义响应格式
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@
|
|||||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<PackageVersion>5.0.2</PackageVersion>
|
<PackageVersion>5.0.2</PackageVersion>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
+2
-3
@@ -9,8 +9,8 @@ using Microsoft.Extensions.Logging;
|
|||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
using SimApi.Logger;
|
using SimApi.Logger;
|
||||||
|
|
||||||
namespace SimApi
|
namespace SimApi;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加入系统的扩展信息
|
/// 加入系统的扩展信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -262,4 +262,3 @@ namespace SimApi
|
|||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -4,6 +4,7 @@ using System.Text.Encodings.Web;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
|
|
||||||
@@ -16,11 +17,7 @@ public partial class Synapse
|
|||||||
|
|
||||||
private void FireEvent(string eventName, object param)
|
private void FireEvent(string eventName, object param)
|
||||||
{
|
{
|
||||||
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var router = $"event.{Options.AppName}.{eventName}";
|
var router = $"event.{Options.AppName}.{eventName}";
|
||||||
var props = EventClientChannel.CreateBasicProperties();
|
var props = EventClientChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Text.Json;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using RabbitMQ.Client.Events;
|
using RabbitMQ.Client.Events;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
|
|
||||||
@@ -35,13 +36,7 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
mt.Invoke(callClass, pt == typeof(string)
|
mt.Invoke(callClass, pt == typeof(string)
|
||||||
? new object[] { reqBody }
|
? new object[] { reqBody }
|
||||||
: new[]
|
: new[] { JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) });
|
||||||
{
|
|
||||||
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
||||||
})
|
|
||||||
});
|
|
||||||
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
+5
-14
@@ -36,15 +36,11 @@ public partial class Synapse
|
|||||||
RpcClientChannel.BasicConsume(queue, false, "", false, false, null, consumer);
|
RpcClientChannel.BasicConsume(queue, false, "", false, false, null, consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object FireRpc(string app, string action, object param)
|
private string FireRpc(string app, string action, object param)
|
||||||
{
|
{
|
||||||
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var router = $"server.{app}";
|
var router = $"server.{app}";
|
||||||
SimApiBaseResponse response;
|
string response;
|
||||||
var props = RpcClientChannel.CreateBasicProperties();
|
var props = RpcClientChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
props.MessageId = Guid.NewGuid().ToString();
|
props.MessageId = Guid.NewGuid().ToString();
|
||||||
@@ -59,17 +55,12 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
||||||
{
|
{
|
||||||
response = new SimApiBaseResponse(502, "timeout");
|
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
||||||
{
|
{
|
||||||
response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>(
|
response = Encoding.UTF8.GetString(value);
|
||||||
Encoding.UTF8.GetString(value), new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
|
||||||
});
|
|
||||||
ResponseCache.Remove(props.MessageId);
|
ResponseCache.Remove(props.MessageId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-11
@@ -4,6 +4,7 @@ using System.Reflection;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -48,10 +49,7 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
var paramObj = JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
||||||
});
|
|
||||||
param = new[] { paramObj };
|
param = new[] { paramObj };
|
||||||
}
|
}
|
||||||
var ret = mt.Invoke(callClass, param);
|
var ret = mt.Invoke(callClass, param);
|
||||||
@@ -59,9 +57,8 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
catch (TargetInvocationException e)
|
catch (TargetInvocationException e)
|
||||||
{
|
{
|
||||||
if (e.InnerException is SimApiException)
|
if (e.InnerException is SimApiException ie)
|
||||||
{
|
{
|
||||||
var ie = e.InnerException as SimApiException;
|
|
||||||
Logger.LogDebug("RPC调用错误: {Err}", ie.Message);
|
Logger.LogDebug("RPC调用错误: {Err}", ie.Message);
|
||||||
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
||||||
}
|
}
|
||||||
@@ -77,11 +74,7 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var returnJson = JsonSerializer.Serialize((object)res, new JsonSerializerOptions
|
var returnJson = JsonSerializer.Serialize((object)res, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
||||||
var props = RpcServerChannel.CreateBasicProperties();
|
var props = RpcServerChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
|
|||||||
+2
-1
@@ -96,7 +96,8 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
res = FireRpc(appName, method, param);
|
var data = FireRpc(appName, method, param);
|
||||||
|
res = JsonSerializer.Deserialize<SimApiBaseResponse<T>>(data, SimApiUtil.JsonOption);
|
||||||
}
|
}
|
||||||
return res as SimApiBaseResponse<T>;
|
return res as SimApiBaseResponse<T>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user