Compare commits

..
9 Commits
Author SHA1 Message Date
xrain b5e4d57772 update package 2023-02-05 19:43:39 +08:00
xrain 239cd8bcbb fix error when exception response has started 2022-09-14 04:56:21 +08:00
xrain 62ddd6698e now simapiauth can use , for role 2022-09-02 09:47:41 +08:00
xrain b2a6e5f40f make storage bucket public 2022-08-17 20:26:25 +08:00
xrain 801f698dcf add storage get url 2022-08-17 19:37:39 +08:00
xrain 9c939426ba fix logger 2022-08-05 02:33:47 +08:00
xrain e45cc4d28d fix logger 2022-08-05 02:20:27 +08:00
xrain 6023cf2eb0 fix default option, add option description 2022-06-29 03:03:51 +08:00
xrain 63dcd0f13f update minio, default all option to false; 2022-06-29 02:50:15 +08:00
7 changed files with 125 additions and 47 deletions
+10 -4
View File
@@ -19,13 +19,16 @@ namespace SimApi.Attributes
//默认是user登录类型 //默认是user登录类型
public SimApiAuthAttribute() public SimApiAuthAttribute()
{ {
Types = new[] { "user" }; Types = new[]
{
"user"
};
} }
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type) public SimApiAuthAttribute(string type)
{ {
Types = new[] { type }; Types = type.Split(",");
} }
//设定特定类型的检测 //设定特定类型的检测
@@ -37,7 +40,10 @@ namespace SimApi.Attributes
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url) public SimApiAuthAttribute(string type, string url)
{ {
Types = new[] { type }; Types = new[]
{
type
};
new HttpPostAttribute(url); new HttpPostAttribute(url);
} }
@@ -50,7 +56,7 @@ namespace SimApi.Attributes
throw new SimApiException(401); throw new SimApiException(401);
} }
//检测用户类型 //检测用户类型
if (Types.Intersect(loginInfo.Type).Count() == 0) if (!Types.Intersect(loginInfo.Type).Any())
{ {
throw new SimApiException(403); throw new SimApiException(403);
} }
+49 -2
View File
@@ -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();
+13
View File
@@ -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; }
} }
} }
+21 -18
View File
@@ -15,7 +15,7 @@ namespace SimApi.Helpers
private string ServeUrl { get; } private string ServeUrl { get; }
private string Bucket { get; } public string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; } private IHttpContextAccessor HttpContextAccessor { get; }
@@ -49,41 +49,44 @@ namespace SimApi.Helpers
} }
Mc = mcb.Build(); Mc = mcb.Build();
bool found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result; var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
if (!found) if (!found)
{ {
Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait(); Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait();
} }
} }
/// <summary>
/// 获取上传的URL
/// </summary>
/// <param name="path"></param>
/// <param name="expire"></param>
/// <returns></returns>
public string GetUploadUrl(string path, int expire = 7200) public string GetUploadUrl(string path, int expire = 7200)
{
try
{ {
return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket) return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
.WithObject(path).WithExpiry(expire)).Result; .WithObject(path).WithExpiry(expire)).Result;
} }
catch (MinioException e)
/// <summary>
/// 获取下载的URL
/// </summary>
/// <param name="path"></param>
/// <param name="expire"></param>
/// <returns></returns>
public string GetDownloadUrl(string path, int expire = 600)
{ {
Console.WriteLine("Error occurred: " + e); return Mc.PresignedGetObjectAsync(new PresignedGetObjectArgs().WithBucket(Bucket).WithObject(path)
return e.Message; .WithExpiry(expire)).Result;
}
} }
public string UploadFile(string path, Stream stream, string contentType = "image/png") public string UploadFile(string path, Stream stream, string contentType = "image/png")
{ {
try Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).WithObjectSize(stream.Length)
{ .WithStreamData(stream).WithContentType(contentType)).Wait();
Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).
WithObjectSize(stream.Length).WithStreamData(stream).WithContentType(contentType)).Wait();
return null; return null;
} }
catch (MinioException e)
{
Console.WriteLine("Error occurred: " + e);
return e.Message;
}
}
public string FullUrl(string path) public string FullUrl(string path)
{ {
+8 -1
View File
@@ -14,6 +14,7 @@ namespace SimApi.Middlewares
public class SimApiExceptionMiddleware public class SimApiExceptionMiddleware
{ {
private RequestDelegate Next { get; } private RequestDelegate Next { get; }
private ILogger<SimApiExceptionMiddleware> Log { get; } private ILogger<SimApiExceptionMiddleware> Log { get; }
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log) public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
@@ -35,7 +36,10 @@ namespace SimApi.Middlewares
await Next(context); await Next(context);
if (context.Response.StatusCode != 200) if (context.Response.StatusCode != 200)
{ {
if (!new[] {301, 302}.Contains(context.Response.StatusCode)) if (!new[]
{
301, 302
}.Contains(context.Response.StatusCode))
{ {
throw new SimApiException(context.Response.StatusCode); throw new SimApiException(context.Response.StatusCode);
} }
@@ -69,6 +73,8 @@ namespace SimApi.Middlewares
/// <param name="context"></param> /// <param name="context"></param>
/// <param name="response"></param> /// <param name="response"></param>
private void ErrorResponse(HttpContext context, SimApiBaseResponse response) private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{
if (!context.Response.HasStarted)
{ {
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json"); context.Response.Headers.Add("Content-Type", "application/json");
@@ -76,3 +82,4 @@ namespace SimApi.Middlewares
} }
} }
} }
}
+4 -4
View File
@@ -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>
<TargetFramework>net6.0</TargetFramework> <TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -27,9 +27,9 @@
<Folder Include="Configurations\" /> <Folder Include="Configurations\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Minio" Version="4.0.2" /> <PackageReference Include="Minio" Version="4.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.3.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup> </ItemGroup>
<ProjectExtensions> <ProjectExtensions>
<MonoDevelop> <MonoDevelop>
+9 -7
View File
@@ -22,7 +22,14 @@ namespace SimApi
{ {
var simApiOptions = new SimApiOptions(); var simApiOptions = new SimApiOptions();
options?.Invoke(simApiOptions); options?.Invoke(simApiOptions);
if (simApiOptions.EnableLogger)
{
builder.AddLogging(logger =>
{
logger.ClearProviders();
logger.AddProvider(new SimApiLoggerProvider());
});
}
// 是否使用 AUTH // 是否使用 AUTH
if (simApiOptions.EnableSimApiAuth) if (simApiOptions.EnableSimApiAuth)
{ {
@@ -190,13 +197,8 @@ namespace SimApi
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder) public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
{ {
var options = builder.ApplicationServices.GetRequiredService<SimApiOptions>(); var options = builder.ApplicationServices.GetRequiredService<SimApiOptions>();
if (options.EnableLogger)
{
builder.ApplicationServices.GetRequiredService<ILoggerFactory>()
.AddProvider(new SimApiLoggerProvider());
}
var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiLogger>>(); var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiOptions>>();
if (options.EnableForwardHeaders) if (options.EnableForwardHeaders)
{ {