Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c27011deea | ||
|
|
b545af2365 | ||
|
|
ac9fd706f9 |
@@ -11,6 +11,7 @@ namespace SimApi.Configs
|
||||
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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
+24
-7
@@ -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
|
||||
{
|
||||
@@ -93,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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,27 +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>();
|
||||
|
||||
if (options.EnableForwardHeaders)
|
||||
{
|
||||
SimApiUtil.Log("开始配置ForwardedHeaders...");
|
||||
logger.LogInformation("开始配置ForwardedHeaders...");
|
||||
builder.UseForwardedHeaders();
|
||||
}
|
||||
|
||||
if (options.EnableCors)
|
||||
{
|
||||
SimApiUtil.Log("开始配置Cors全部允许...");
|
||||
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 =>
|
||||
{
|
||||
@@ -198,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;
|
||||
|
||||
Reference in New Issue
Block a user