Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6023cf2eb0 | ||
|
|
63dcd0f13f | ||
|
|
22704590f5 | ||
|
|
98907ba5af | ||
|
|
7b0f0a9245 | ||
|
|
1c4fb06163 | ||
|
|
e79ed47d39 | ||
|
|
6fdb3da059 |
@@ -4,15 +4,62 @@ namespace SimApi.Configs
|
|||||||
{
|
{
|
||||||
public class SimApiOptions
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 启用全部Cors,对于开发前后分离的时候很有用。
|
||||||
|
/// default: true
|
||||||
|
/// </summary>
|
||||||
public bool EnableCors { get; set; } = true;
|
public bool EnableCors { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用SimapiAuth,一个简单的基于Header Token的认证方式。
|
||||||
|
/// default: false
|
||||||
|
/// </summary>
|
||||||
public bool EnableSimApiAuth { get; set; } = false;
|
public bool EnableSimApiAuth { get; set; } = false;
|
||||||
public bool EnableSimApiDoc { get; set; } = true;
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用在线文档,启用后 访问 /swagger 可以查看对应的api文档。
|
||||||
|
/// default: false
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableSimApiDoc { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用异常拦截,启用后,所有的异常将被通过json反馈。
|
||||||
|
/// default: true
|
||||||
|
/// </summary>
|
||||||
public bool EnableSimApiException { get; set; } = true;
|
public bool EnableSimApiException { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开启S3兼容的存储系统。
|
||||||
|
/// default: false
|
||||||
|
/// </summary>
|
||||||
public bool EnableSimApiStorage { get; set; } = false;
|
public bool EnableSimApiStorage { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开启ForwardHeaders,开启后可以透传负载均衡的Headers
|
||||||
|
/// default: true
|
||||||
|
/// </summary>
|
||||||
public bool EnableForwardHeaders { get; set; } = true;
|
public bool EnableForwardHeaders { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将所有的url都格式化为小写字母
|
||||||
|
/// default: true
|
||||||
|
/// </summary>
|
||||||
public bool EnableLowerUrl { get; set; } = true;
|
public bool EnableLowerUrl { get; set; } = true;
|
||||||
public bool EnableLogger { get; set; } = true;
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用格式化的 Console Logger
|
||||||
|
/// default: false
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableLogger { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Swagger文档相关配置,需要启用 EnableSimApiDoc
|
||||||
|
/// </summary>
|
||||||
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// S3兼容的存储系统配置,需要启用 EnableSimApiStorage
|
||||||
|
/// </summary>
|
||||||
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,23 @@ namespace SimApi.Configs
|
|||||||
{
|
{
|
||||||
public class SimApiStorageOptions
|
public class SimApiStorageOptions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// S3 服务器入口地址
|
||||||
|
/// </summary>
|
||||||
public string Endpoint { get; set; }
|
public string Endpoint { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// S3 服务器文件访问地址
|
||||||
|
/// </summary>
|
||||||
public string ServeUrl { get; set; }
|
public string ServeUrl { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// S3服务 Bucket
|
||||||
|
/// </summary>
|
||||||
public string Bucket { get; set; }
|
public string Bucket { get; set; }
|
||||||
|
|
||||||
public string AccessKey { get; set; }
|
public string AccessKey { get; set; }
|
||||||
|
|
||||||
public string SecretKey { get; set; }
|
public string SecretKey { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-13
@@ -14,7 +14,9 @@ namespace SimApi.Helpers
|
|||||||
public MinioClient Client => Mc;
|
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; }
|
||||||
|
|
||||||
public SimApiStorage(SimApiOptions apiOptions, IHttpContextAccessor httpContextAccessor)
|
public SimApiStorage(SimApiOptions apiOptions, IHttpContextAccessor httpContextAccessor)
|
||||||
@@ -22,7 +24,7 @@ namespace SimApi.Helpers
|
|||||||
var options = apiOptions.SimApiStorageOptions;
|
var options = apiOptions.SimApiStorageOptions;
|
||||||
HttpContextAccessor = httpContextAccessor;
|
HttpContextAccessor = httpContextAccessor;
|
||||||
var useSsl = false;
|
var useSsl = false;
|
||||||
var endpoint = string.Empty;
|
string endpoint;
|
||||||
if (options.Endpoint.StartsWith("http://"))
|
if (options.Endpoint.StartsWith("http://"))
|
||||||
{
|
{
|
||||||
endpoint = options.Endpoint.Replace("http://", string.Empty);
|
endpoint = options.Endpoint.Replace("http://", string.Empty);
|
||||||
@@ -39,19 +41,18 @@ namespace SimApi.Helpers
|
|||||||
|
|
||||||
ServeUrl = options.ServeUrl;
|
ServeUrl = options.ServeUrl;
|
||||||
Bucket = options.Bucket;
|
Bucket = options.Bucket;
|
||||||
|
var mcb = new MinioClient().WithEndpoint(endpoint)
|
||||||
|
.WithCredentials(options.AccessKey, options.SecretKey);
|
||||||
if (useSsl)
|
if (useSsl)
|
||||||
{
|
{
|
||||||
Mc = new MinioClient(endpoint, options.AccessKey, options.SecretKey).WithSSL();
|
mcb = mcb.WithSSL();
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Mc = new MinioClient(endpoint, options.AccessKey, options.SecretKey);
|
|
||||||
}
|
}
|
||||||
|
Mc = mcb.Build();
|
||||||
|
|
||||||
bool found = Mc.BucketExistsAsync(Bucket).Result;
|
bool found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
Mc.MakeBucketAsync(Bucket).Wait();
|
Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +60,8 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Mc.PresignedPutObjectAsync(Bucket, path, expire).Result;
|
return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
|
||||||
|
.WithObject(path).WithExpiry(expire)).Result;
|
||||||
}
|
}
|
||||||
catch (MinioException e)
|
catch (MinioException e)
|
||||||
{
|
{
|
||||||
@@ -68,11 +70,12 @@ namespace SimApi.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string UploadFile(string path, Stream stream)
|
public string UploadFile(string path, Stream stream,string contentType = "image/png")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Mc.PutObjectAsync(Bucket, path, stream, stream.Length).Wait();
|
Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).
|
||||||
|
WithObjectSize(stream.Length).WithStreamData(stream).WithContentType(contentType)).Wait();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (MinioException e)
|
catch (MinioException e)
|
||||||
@@ -84,8 +87,8 @@ namespace SimApi.Helpers
|
|||||||
|
|
||||||
public string FullUrl(string path)
|
public string FullUrl(string path)
|
||||||
{
|
{
|
||||||
var httpRequest = HttpContextAccessor.HttpContext.Request;
|
var httpRequest = HttpContextAccessor.HttpContext?.Request;
|
||||||
var url = $"{httpRequest.Scheme}://{httpRequest.Host}";
|
var url = $"{httpRequest?.Scheme}://{httpRequest?.Host}";
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
return path;
|
return path;
|
||||||
|
|||||||
+34
-1
@@ -1,11 +1,31 @@
|
|||||||
using System.Security.Cryptography;
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Text.Unicode;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers
|
||||||
{
|
{
|
||||||
public static class SimApiUtil
|
public static class SimApiUtil
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 当前CST时间
|
||||||
|
/// </summary>
|
||||||
|
public static DateTime CstNow
|
||||||
|
{
|
||||||
|
get => DateTime.UtcNow.AddHours(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前秒级时间戳
|
||||||
|
/// </summary>
|
||||||
|
public static double TimestampNow
|
||||||
|
{
|
||||||
|
get => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测手机号是否正确
|
/// 检测手机号是否正确
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -36,5 +56,18 @@ namespace SimApi.Helpers
|
|||||||
|
|
||||||
return strbul.ToString();
|
return strbul.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将对象序列化成JSON (控制台输出中文不会被编码)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Json(object obj)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(obj, new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Logger
|
namespace SimApi.Logger
|
||||||
{
|
{
|
||||||
@@ -30,7 +31,7 @@ namespace SimApi.Logger
|
|||||||
_ => defautColor
|
_ => defautColor
|
||||||
};
|
};
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
$"[ {Name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]");
|
$"[ {Name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]");
|
||||||
Console.ForegroundColor = defautColor;
|
Console.ForegroundColor = defautColor;
|
||||||
Console.WriteLine($"{state}");
|
Console.WriteLine($"{state}");
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace SimApi.Logger;
|
||||||
|
|
||||||
|
public class SimApiLoggerConfiguration
|
||||||
|
{
|
||||||
|
public int EventId { get; set; }
|
||||||
|
|
||||||
|
public Dictionary<LogLevel, ConsoleColor> LogLevels { get; set; } = new()
|
||||||
|
{
|
||||||
|
[LogLevel.Information] = ConsoleColor.Green
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,23 +1,36 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Models
|
namespace SimApi.Models
|
||||||
{
|
{
|
||||||
public class SimApiBaseModel
|
public class SimApiBaseModel
|
||||||
{
|
{
|
||||||
protected virtual string[] MapperIgnoreField { get; set; } = {"Id", "CreatedAt", "UpdatedAt"};
|
protected virtual string[] MapperIgnoreField { get; set; } =
|
||||||
|
{
|
||||||
|
"Id", "CreatedAt", "UpdatedAt"
|
||||||
|
};
|
||||||
|
|
||||||
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
||||||
|
|
||||||
public void MapData<TS>(TS source, bool mapAll = false)
|
public void MapData<TS>(TS source, bool mapAll = false)
|
||||||
{
|
{
|
||||||
//获取要赋值的源数据不为null的项目
|
//获取要赋值的源数据不为null的项目
|
||||||
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
||||||
.Select(x => new {x.Name, x.PropertyType})
|
.Select(x => new
|
||||||
|
{
|
||||||
|
x.Name,
|
||||||
|
x.PropertyType
|
||||||
|
})
|
||||||
.ToDictionary(x => x.Name, x => x.PropertyType);
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
|
||||||
//获取目标对象的属性信息
|
//获取目标对象的属性信息
|
||||||
var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType})
|
var targetProps = GetType().GetProperties().Select(x => new
|
||||||
|
{
|
||||||
|
x.Name,
|
||||||
|
x.PropertyType
|
||||||
|
})
|
||||||
.ToDictionary(x => x.Name, x => x.PropertyType);
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
foreach (var sp in sourceProps)
|
foreach (var sp in sourceProps)
|
||||||
{
|
{
|
||||||
@@ -37,11 +50,19 @@ namespace SimApi.Models
|
|||||||
{
|
{
|
||||||
//获取要赋值的源数据不为null的项目
|
//获取要赋值的源数据不为null的项目
|
||||||
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
||||||
.Select(x => new {x.Name, x.PropertyType})
|
.Select(x => new
|
||||||
|
{
|
||||||
|
x.Name,
|
||||||
|
x.PropertyType
|
||||||
|
})
|
||||||
.ToDictionary(x => x.Name, x => x.PropertyType);
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
|
||||||
//获取目标对象的属性信息
|
//获取目标对象的属性信息
|
||||||
var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType})
|
var targetProps = GetType().GetProperties().Select(x => new
|
||||||
|
{
|
||||||
|
x.Name,
|
||||||
|
x.PropertyType
|
||||||
|
})
|
||||||
.ToDictionary(x => x.Name, x => x.PropertyType);
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
foreach (var sp in sourceProps)
|
foreach (var sp in sourceProps)
|
||||||
{
|
{
|
||||||
@@ -63,7 +84,7 @@ namespace SimApi.Models
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public void UpdateTime()
|
public void UpdateTime()
|
||||||
{
|
{
|
||||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now);
|
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
"anonymousAuthentication": true
|
"anonymousAuthentication": true
|
||||||
},
|
},
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"YYApi": {
|
"SimApi": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
|
|||||||
+3
-4
@@ -17,7 +17,6 @@
|
|||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(RunConfiguration)' == 'YYApi' " />
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Helpers\" />
|
<Folder Include="Helpers\" />
|
||||||
<Folder Include="Communications\" />
|
<Folder Include="Communications\" />
|
||||||
@@ -28,9 +27,9 @@
|
|||||||
<Folder Include="Configurations\" />
|
<Folder Include="Configurations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="3.1.13" />
|
<PackageReference Include="Minio" Version="4.0.4" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.3.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
Reference in New Issue
Block a user