Compare commits

...
12 Commits
Author SHA1 Message Date
xrain 98ec9ae231 update to .net6 2021-11-14 15:43:23 +08:00
xrain 18d2dfb86e add get upload url 2021-09-29 02:37:14 +08:00
xrain ef3f1bfa88 fix checkcell regex 2021-09-26 13:11:22 +08:00
xrain 01a2689549 fix ci 2021-07-10 18:39:32 +08:00
xrainandGitHub 916f046789 fix ci 2021-07-10 18:39:02 +08:00
xrain 51ac170a22 add logger 2021-06-28 05:45:29 +08:00
xrain c27011deea add logger 2021-06-28 05:36:54 +08:00
xrain b545af2365 add password oauth method for doc 2021-06-27 10:59:19 +08:00
xrain ac9fd706f9 add map special field method 2021-06-08 10:54:51 +08:00
xrain 8b4d551af8 storage add client 2021-06-07 15:24:22 +08:00
xrain 3445531972 add cors 2021-06-05 14:33:59 +08:00
xrain 2268ad0c8c add cors 2021-06-05 14:30:52 +08:00
9 changed files with 158 additions and 20 deletions
@@ -1,6 +1,9 @@
name: PublishNugetPackage
on: [create]
on:
push:
tags:
- '*'
jobs:
publish:
+2
View File
@@ -4,12 +4,14 @@ namespace SimApi.Configs
{
public class SimApiOptions
{
public bool EnableCors { get; set; } = true;
public bool EnableSimApiAuth { get; set; } = false;
public bool EnableSimApiDoc { get; set; } = true;
public bool EnableSimApiException { get; set; } = true;
public bool EnableSimApiStorage { get; set; } = false;
public bool EnableForwardHeaders { get; set; } = true;
public bool EnableLowerUrl { get; set; } = true;
public bool EnableLogger { get; set; } = true;
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
+16
View File
@@ -10,6 +10,9 @@ namespace SimApi.Helpers
public class SimApiStorage
{
private MinioClient Mc { get; }
public MinioClient Client => Mc;
private string ServeUrl { get; }
private string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; }
@@ -52,6 +55,19 @@ namespace SimApi.Helpers
}
}
public string GetUploadUrl(string path, int expire = 7200)
{
try
{
return Mc.PresignedPutObjectAsync(Bucket, path, expire).Result;
}
catch (MinioException e)
{
Console.WriteLine("Error occurred: " + e);
return e.Message;
}
}
public string UploadFile(string path, Stream stream)
{
try
+2 -10
View File
@@ -1,5 +1,4 @@
using System;
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
@@ -14,7 +13,7 @@ namespace SimApi.Helpers
/// <returns></returns>
public static bool CheckCell(string cell)
{
var regex = new Regex("^1[34578]\\d{9}$");
var regex = new Regex("^1[3456789]\\d{9}$");
return regex.IsMatch(cell);
}
@@ -37,12 +36,5 @@ namespace SimApi.Helpers
return strbul.ToString();
}
public static void Log(string message, string type = "Information")
{
Console.WriteLine($"[ SimApi ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {type} ]");
Console.WriteLine(message);
Console.WriteLine("");
}
}
}
+50
View File
@@ -0,0 +1,50 @@
using System;
using Microsoft.Extensions.Logging;
namespace SimApi.Logger
{
public class SimApiLogger : ILogger
{
private string Name { get; }
public SimApiLogger(string name)
{
Name = name;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
Func<TState, Exception, string> formatter)
{
var defautColor = Console.ForegroundColor;
Console.ForegroundColor = logLevel switch
{
LogLevel.Debug => ConsoleColor.DarkMagenta,
LogLevel.Information => ConsoleColor.DarkCyan,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Error => ConsoleColor.Red,
LogLevel.Critical => ConsoleColor.DarkRed,
_ => defautColor
};
Console.WriteLine(
$"[ {Name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]");
Console.ForegroundColor = defautColor;
Console.WriteLine($"{state}");
if (exception != null)
{
Console.WriteLine($"{exception}");
}
Console.WriteLine();
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace SimApi.Logger
{
public class SimApiLoggerProvider : ILoggerProvider
{
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers =
new ConcurrentDictionary<string, SimApiLogger>();
public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new SimApiLogger(name));
}
public void Dispose()
{
_loggers.Clear();
}
}
}
+24
View File
@@ -33,6 +33,30 @@ namespace SimApi.Models
UpdateTime();
}
public void MapData<TS>(TS source, string[] mapFields)
{
//获取要赋值的源数据不为null的项目
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
.Select(x => new {x.Name, x.PropertyType})
.ToDictionary(x => x.Name, x => x.PropertyType);
//获取目标对象的属性信息
var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType})
.ToDictionary(x => x.Name, x => x.PropertyType);
foreach (var sp in sourceProps)
{
//检查源对象不为空的属性是否在目标对象中存在
if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
mapFields.Contains(sp.Key))
{
GetType().GetProperty(sp.Key)?
.SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
}
}
UpdateTime();
}
/// <summary>
/// 更新update时间
/// </summary>
+3 -3
View File
@@ -14,7 +14,7 @@
<SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
@@ -29,8 +29,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Minio" Version="3.1.13" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
+35 -6
View File
@@ -5,7 +5,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using SimApi.Middlewares;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Logging;
using SimApi.Configs;
using SimApi.Logger;
namespace SimApi
{
@@ -27,6 +29,12 @@ namespace SimApi
builder.AddScoped<SimApiAuth>();
}
if (simApiOptions.EnableCors)
{
builder.AddCors(cors => cors.AddPolicy("any",
policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));
}
// 使用SimApiDoc
if (simApiOptions.EnableSimApiDoc)
{
@@ -87,6 +95,14 @@ namespace SimApi
};
haveOauth = true;
break;
case "Password":
oauthFlows.Password = new OpenApiOAuthFlow
{
TokenUrl = new Uri(docOptions.ApiAuth.TokenUrl, UriKind.RelativeOrAbsolute),
Scopes = docOptions.ApiAuth.Scopes
};
haveOauth = true;
break;
}
}
@@ -154,21 +170,34 @@ namespace SimApi
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
{
var options = builder.ApplicationServices.GetService<SimApiOptions>();
if (options.EnableLogger)
{
builder.ApplicationServices.GetService<ILoggerFactory>().AddProvider(new SimApiLoggerProvider());
}
var logger = builder.ApplicationServices.GetService<ILogger<SimApiLogger>>();
if (options.EnableForwardHeaders)
{
SimApiUtil.Log("开始配置ForwardedHeaders...");
logger.LogInformation("开始配置ForwardedHeaders...");
builder.UseForwardedHeaders();
}
if (options.EnableCors)
{
logger.LogInformation("开始配置Cors全部允许...");
builder.UseCors("any");
}
if (options.EnableSimApiAuth)
{
SimApiUtil.Log("开始配置SimApiAuth...");
logger.LogInformation("开始配置SimApiAuth...");
builder.UseMiddleware<SimApiAuthMiddleware>();
}
if (options.EnableSimApiDoc)
{
SimApiUtil.Log("开始配置SimApiDoc...");
logger.LogInformation("开始配置SimApiDoc...");
var docOptions = options.SimApiDocOptions;
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
{
@@ -186,20 +215,20 @@ namespace SimApi
if (options.EnableSimApiException)
{
SimApiUtil.Log("开始配置SimApiException...");
logger.LogInformation("开始配置SimApiException...");
builder.UseMiddleware<SimApiExceptionMiddleware>();
}
//请求一下检测存储错误
if (options.EnableSimApiStorage)
{
SimApiUtil.Log("开始配置SimApiStorage...");
logger.LogInformation("开始配置SimApiStorage...");
builder.ApplicationServices.GetService<SimApiStorage>();
}
if (options.EnableLowerUrl)
{
SimApiUtil.Log("开始配置使用URL小写...");
logger.LogInformation("开始配置使用URL小写...");
}
return builder;