Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c27011deea | ||
|
|
b545af2365 | ||
|
|
ac9fd706f9 | ||
|
|
8b4d551af8 | ||
|
|
3445531972 | ||
|
|
2268ad0c8c |
@@ -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; }
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,30 @@ namespace SimApi.Models
|
|||||||
UpdateTime();
|
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>
|
/// <summary>
|
||||||
/// 更新update时间
|
/// 更新update时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
+35
-6
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,21 +170,34 @@ 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>();
|
||||||
|
|
||||||
if (options.EnableForwardHeaders)
|
if (options.EnableForwardHeaders)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置ForwardedHeaders...");
|
logger.LogInformation("开始配置ForwardedHeaders...");
|
||||||
builder.UseForwardedHeaders();
|
builder.UseForwardedHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.EnableCors)
|
||||||
|
{
|
||||||
|
logger.LogInformation("开始配置Cors全部允许...");
|
||||||
|
builder.UseCors("any");
|
||||||
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiAuth)
|
if (options.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiAuth...");
|
logger.LogInformation("开始配置SimApiAuth...");
|
||||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiDoc)
|
if (options.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiDoc...");
|
logger.LogInformation("开始配置SimApiDoc...");
|
||||||
var docOptions = options.SimApiDocOptions;
|
var docOptions = options.SimApiDocOptions;
|
||||||
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
||||||
{
|
{
|
||||||
@@ -186,20 +215,20 @@ namespace SimApi
|
|||||||
|
|
||||||
if (options.EnableSimApiException)
|
if (options.EnableSimApiException)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiException...");
|
logger.LogInformation("开始配置SimApiException...");
|
||||||
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
//请求一下检测存储错误
|
//请求一下检测存储错误
|
||||||
if (options.EnableSimApiStorage)
|
if (options.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiStorage...");
|
logger.LogInformation("开始配置SimApiStorage...");
|
||||||
builder.ApplicationServices.GetService<SimApiStorage>();
|
builder.ApplicationServices.GetService<SimApiStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableLowerUrl)
|
if (options.EnableLowerUrl)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置使用URL小写...");
|
logger.LogInformation("开始配置使用URL小写...");
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user