Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74c6cb6027 | ||
|
|
98ec9ae231 | ||
|
|
18d2dfb86e | ||
|
|
ef3f1bfa88 | ||
|
|
01a2689549 | ||
|
|
916f046789 | ||
|
|
51ac170a22 | ||
|
|
c27011deea | ||
|
|
b545af2365 | ||
|
|
ac9fd706f9 | ||
|
|
8b4d551af8 | ||
|
|
3445531972 | ||
|
|
2268ad0c8c | ||
|
|
0b6c6af51d | ||
|
|
1c8ea86fd3 | ||
|
|
f5e4cc97ba | ||
|
|
8550d663de | ||
|
|
a6f32c6e43 |
@@ -1,21 +0,0 @@
|
|||||||
name: PublishNugetPackage
|
|
||||||
|
|
||||||
on: [create]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
name: Publish Project to Nuget
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v1
|
|
||||||
- name: Setup .NET Core
|
|
||||||
uses: actions/setup-dotnet@v1
|
|
||||||
with:
|
|
||||||
dotnet-version: '5.0.100'
|
|
||||||
- name: Publish
|
|
||||||
run: |
|
|
||||||
version=`git describe --tags`
|
|
||||||
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
|
|
||||||
env:
|
|
||||||
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
name: PublishNugetPackage
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Publish Project to Nuget
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- name: Setup .NET Core
|
||||||
|
uses: actions/setup-dotnet@v1
|
||||||
|
with:
|
||||||
|
dotnet-version: "6.0.100"
|
||||||
|
- name: Publish
|
||||||
|
run: |
|
||||||
|
version=`git describe --tags`
|
||||||
|
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
|
||||||
|
env:
|
||||||
|
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimApi.Communications
|
namespace SimApi.Communications
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -9,6 +10,15 @@ namespace SimApi.Communications
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 动态类型单字段请求
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public class SimApiOneFieldRequest<T>
|
||||||
|
{
|
||||||
|
public T Data { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 基础分页请求
|
/// 基础分页请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ namespace SimApi.Configs
|
|||||||
{
|
{
|
||||||
public class SimApiOptions
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
|
public bool EnableCors { get; set; } = true;
|
||||||
public bool EnableSimApiAuth { get; set; } = false;
|
public bool EnableSimApiAuth { get; set; } = false;
|
||||||
public bool EnableSimApiDoc { get; set; } = true;
|
public bool EnableSimApiDoc { get; set; } = true;
|
||||||
public bool EnableSimApiException { get; set; } = true;
|
public bool EnableSimApiException { get; set; } = true;
|
||||||
public bool EnableSimApiStorage { get; set; } = false;
|
public bool EnableSimApiStorage { get; set; } = false;
|
||||||
public bool EnableForwardHeaders { get; set; } = true;
|
public bool EnableForwardHeaders { get; set; } = true;
|
||||||
public bool EnableLowerUrl { get; set; } = true;
|
public bool EnableLowerUrl { get; set; } = true;
|
||||||
|
public bool EnableLogger { get; set; } = true;
|
||||||
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
||||||
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ namespace SimApi.Helpers
|
|||||||
public class SimApiStorage
|
public class SimApiStorage
|
||||||
{
|
{
|
||||||
private MinioClient Mc { get; }
|
private MinioClient Mc { get; }
|
||||||
|
|
||||||
|
public MinioClient Client => Mc;
|
||||||
|
|
||||||
private string ServeUrl { get; }
|
private string ServeUrl { get; }
|
||||||
private string Bucket { get; }
|
private string Bucket { get; }
|
||||||
private IHttpContextAccessor HttpContextAccessor { 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)
|
public string UploadFile(string path, Stream stream)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Security.Cryptography;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@@ -14,7 +13,7 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool CheckCell(string cell)
|
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);
|
return regex.IsMatch(cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace SimApi.Models
|
||||||
|
{
|
||||||
|
public class SimApiBaseModel
|
||||||
|
{
|
||||||
|
protected virtual string[] MapperIgnoreField { get; set; } = {"Id", "CreatedAt", "UpdatedAt"};
|
||||||
|
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
||||||
|
|
||||||
|
public void MapData<TS>(TS source, bool mapAll = false)
|
||||||
|
{
|
||||||
|
//获取要赋值的源数据不为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] &&
|
||||||
|
(!MapperIgnoreField.Contains(sp.Key) || mapAll))
|
||||||
|
{
|
||||||
|
GetType().GetProperty(sp.Key)?
|
||||||
|
.SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
|
/// <returns></returns>
|
||||||
|
public void UpdateTime()
|
||||||
|
{
|
||||||
|
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-3
@@ -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>
|
||||||
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
|
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
|
||||||
@@ -29,8 +29,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="3.1.13" />
|
<PackageReference Include="Minio" Version="3.1.13" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.1.4" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.1.4" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
+49
-3
@@ -5,7 +5,9 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using SimApi.Middlewares;
|
using SimApi.Middlewares;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
|
using SimApi.Logger;
|
||||||
|
|
||||||
namespace SimApi
|
namespace SimApi
|
||||||
{
|
{
|
||||||
@@ -27,6 +29,12 @@ namespace SimApi
|
|||||||
builder.AddScoped<SimApiAuth>();
|
builder.AddScoped<SimApiAuth>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (simApiOptions.EnableCors)
|
||||||
|
{
|
||||||
|
builder.AddCors(cors => cors.AddPolicy("any",
|
||||||
|
policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));
|
||||||
|
}
|
||||||
|
|
||||||
// 使用SimApiDoc
|
// 使用SimApiDoc
|
||||||
if (simApiOptions.EnableSimApiDoc)
|
if (simApiOptions.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
@@ -87,6 +95,14 @@ namespace SimApi
|
|||||||
};
|
};
|
||||||
haveOauth = true;
|
haveOauth = true;
|
||||||
break;
|
break;
|
||||||
|
case "Password":
|
||||||
|
oauthFlows.Password = new OpenApiOAuthFlow
|
||||||
|
{
|
||||||
|
TokenUrl = new Uri(docOptions.ApiAuth.TokenUrl, UriKind.RelativeOrAbsolute),
|
||||||
|
Scopes = docOptions.ApiAuth.Scopes
|
||||||
|
};
|
||||||
|
haveOauth = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +154,7 @@ namespace SimApi
|
|||||||
|
|
||||||
if (simApiOptions.EnableSimApiStorage)
|
if (simApiOptions.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
|
builder.AddHttpContextAccessor();
|
||||||
builder.AddSingleton<SimApiStorage>();
|
builder.AddSingleton<SimApiStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,15 +170,36 @@ namespace SimApi
|
|||||||
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var options = builder.ApplicationServices.GetService<SimApiOptions>();
|
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)
|
||||||
|
{
|
||||||
|
logger.LogInformation("开始配置ForwardedHeaders...");
|
||||||
|
builder.UseForwardedHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.EnableCors)
|
||||||
|
{
|
||||||
|
logger.LogInformation("开始配置Cors全部允许...");
|
||||||
|
builder.UseCors("any");
|
||||||
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiAuth)
|
if (options.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
|
logger.LogInformation("开始配置SimApiAuth...");
|
||||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiDoc)
|
if (options.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
|
logger.LogInformation("开始配置SimApiDoc...");
|
||||||
var docOptions = options.SimApiDocOptions;
|
var docOptions = options.SimApiDocOptions;
|
||||||
return builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
||||||
{
|
{
|
||||||
x.DocumentTitle = docOptions.DocumentTitle;
|
x.DocumentTitle = docOptions.DocumentTitle;
|
||||||
foreach (var group in docOptions.ApiGroups)
|
foreach (var group in docOptions.ApiGroups)
|
||||||
@@ -177,12 +215,20 @@ namespace SimApi
|
|||||||
|
|
||||||
if (options.EnableSimApiException)
|
if (options.EnableSimApiException)
|
||||||
{
|
{
|
||||||
|
logger.LogInformation("开始配置SimApiException...");
|
||||||
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableForwardHeaders)
|
//请求一下检测存储错误
|
||||||
|
if (options.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
builder.UseForwardedHeaders();
|
logger.LogInformation("开始配置SimApiStorage...");
|
||||||
|
builder.ApplicationServices.GetService<SimApiStorage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.EnableLowerUrl)
|
||||||
|
{
|
||||||
|
logger.LogInformation("开始配置使用URL小写...");
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user