31 changed files with 447 additions and 68 deletions
@ -1,6 +1,6 @@ |
|||
namespace Lion.AbpPro |
|||
{ |
|||
public class AbpProHttpApiHostConsts |
|||
public static class AbpProHttpApiHostConst |
|||
{ |
|||
/// <summary>
|
|||
/// 跨域策略名
|
|||
@ -0,0 +1,15 @@ |
|||
using Lion.AbpPro.Extensions.Middlewares; |
|||
|
|||
namespace Microsoft.AspNetCore.Builder; |
|||
|
|||
public static class AbpProApplicationBuilderExtensionsExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// 记录请求响应日志
|
|||
/// </summary>
|
|||
/// <returns></returns>
|
|||
public static IApplicationBuilder UseRequestLog(this IApplicationBuilder app) |
|||
{ |
|||
return app.UseMiddleware<RequestLogMiddleware>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,84 @@ |
|||
using System; |
|||
using System.IO; |
|||
using System.Text.RegularExpressions; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Lion.AbpPro.Extensions.Middlewares; |
|||
|
|||
public class RequestLogMiddleware |
|||
{ |
|||
private readonly RequestDelegate _next; |
|||
private readonly ILogger<RequestLogMiddleware> _logger; |
|||
|
|||
public RequestLogMiddleware(RequestDelegate next, |
|||
ILogger<RequestLogMiddleware> logger) |
|||
{ |
|||
_next = next; |
|||
_logger = logger; |
|||
} |
|||
|
|||
public async Task InvokeAsync(HttpContext context) |
|||
{ |
|||
context.Request.EnableBuffering(); |
|||
var originalBody = context.Response.Body; |
|||
if (context.Request.Path.ToString().ToLower().Contains("swagger") |
|||
|| context.Request.Path.ToString().ToLower().Contains("login") |
|||
|| context.Request.Path.ToString().ToLower().Contains("monitor") |
|||
|| context.Request.Path.ToString().ToLower().Contains("cap") |
|||
|| context.Request.Path.ToString().ToLower().Contains("hangfire") |
|||
|| context.Request.Path.ToString() == "/" |
|||
) |
|||
{ |
|||
await _next(context); |
|||
} |
|||
else |
|||
{ |
|||
try |
|||
{ |
|||
var logRequestId = Guid.NewGuid().ToString(); |
|||
await RequestDataLog(context, logRequestId); |
|||
using (var ms = new MemoryStream()) |
|||
{ |
|||
context.Response.Body = ms; |
|||
await _next(context); |
|||
ResponseDataLog(ms, logRequestId); |
|||
ms.Position = 0; |
|||
await ms.CopyToAsync(originalBody); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
// 记录异常
|
|||
_logger.LogError(ex.Message + "" + ex.InnerException); |
|||
} |
|||
finally |
|||
{ |
|||
context.Response.Body = originalBody; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private async Task RequestDataLog(HttpContext context, |
|||
string requestId) |
|||
{ |
|||
var request = context.Request; |
|||
var body = new StreamReader(request.Body); |
|||
var requestData = $" 请求路径:{request.Path}\r\n 请求Body参数:{await body.ReadToEndAsync()}"; |
|||
_logger.LogInformation($"日志中间件[Request],LogRequestId:{requestId}:请求接口信息:{requestData}"); |
|||
request.Body.Position = 0; |
|||
} |
|||
|
|||
private void ResponseDataLog(MemoryStream ms, string requestId) |
|||
{ |
|||
ms.Position = 0; |
|||
var responseBody = new StreamReader(ms).ReadToEnd(); |
|||
// 去除 Html
|
|||
var isHtml = Regex.IsMatch(responseBody, "<[^>]+>"); |
|||
if (!isHtml && !string.IsNullOrEmpty(responseBody)) |
|||
{ |
|||
_logger.LogInformation($"日志中间件[Response],LogRequestId:{requestId}:响应接口信息:{responseBody}"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Lion.AbpPro.Users.Dto |
|||
{ |
|||
public class UserOutput |
|||
{ |
|||
public Guid Id { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using Lion.AbpPro.Users.Dto; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Lion.AbpPro.Users |
|||
{ |
|||
public interface IUserFreeSqlBasicRepository |
|||
{ |
|||
Task<List<UserOutput>> GetListAsync(); |
|||
} |
|||
} |
|||
@ -1,7 +1,7 @@ |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlReppsitory; |
|||
namespace Lion.AbpPro.FreeSqlRepository; |
|||
|
|||
public abstract class FreeSqlBasicRepository : DomainService |
|||
{ |
|||
@ -0,0 +1,17 @@ |
|||
using Lion.AbpPro.Users; |
|||
using Lion.AbpPro.Users.Dto; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlRepository |
|||
{ |
|||
public class UserFreeSqlBasicRepository : FreeSqlBasicRepository, IUserFreeSqlBasicRepository |
|||
{ |
|||
public async Task<List<UserOutput>> GetListAsync() |
|||
{ |
|||
var sql = "select id from AbpUsers"; |
|||
var result = await FreeSql.Select<UserOutput>() |
|||
.WithSql(sql) |
|||
.ToListAsync(); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using Lion.AbpPro.FreeSqlReppsitory.Tests; |
|||
using Lion.AbpPro.Localization; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlRepository.Tests |
|||
{ |
|||
public abstract class AbpProFreeSqlRepositoryTestBase: AbpProTestBase<AbpProFreeSqlRepositoryTestModule> |
|||
{ |
|||
protected AbpProFreeSqlRepositoryTestBase() |
|||
{ |
|||
ServiceProvider.InitializeLocalization(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Lion.AbpPro.EntityFrameworkCore; |
|||
using Lion.AbpPro.FreeSqlRepository; |
|||
using Microsoft.Data.Sqlite; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Storage; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore.Sqlite; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlReppsitory.Tests; |
|||
|
|||
[DependsOn( |
|||
typeof(AbpProTestBaseModule), |
|||
typeof(AbpProEntityFrameworkCoreTestModule), |
|||
typeof(AbpProFreeSqlModule) |
|||
)] |
|||
public class AbpProFreeSqlRepositoryTestModule : AbpModule |
|||
{ |
|||
private const string ConnectionString = "Data Source=:memory:"; |
|||
private SqliteConnection _sqliteConnection; |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
|
|||
var freeSql = new FreeSql.FreeSqlBuilder() |
|||
.UseConnectionString(FreeSql.DataType.Sqlite, ConnectionString) |
|||
.UseAutoSyncStructure(true) |
|||
.Build(); |
|||
context.Services.AddSingleton<IFreeSql>(freeSql); |
|||
var connection = new SqliteConnection(ConnectionString); |
|||
ConfigureInMemorySqlite(context.Services, freeSql); |
|||
} |
|||
|
|||
|
|||
private void ConfigureInMemorySqlite(IServiceCollection services, IFreeSql freeSql) |
|||
{ |
|||
_sqliteConnection = CreateDatabaseAndGetConnection(freeSql); |
|||
|
|||
//services.Configure<AbpDbContextOptions>(options =>
|
|||
//{
|
|||
// options.PreConfigure<AbpProDbContext>(options => { options.DbContextOptions.UseBatchEF_Sqlite(); });
|
|||
// options.Configure(context => { context.DbContextOptions.UseSqlite(_sqliteConnection); });
|
|||
//});
|
|||
} |
|||
|
|||
public override void OnApplicationShutdown(ApplicationShutdownContext context) |
|||
{ |
|||
// _sqliteConnection.Dispose();
|
|||
} |
|||
|
|||
private static SqliteConnection CreateDatabaseAndGetConnection(IFreeSql freeSql) |
|||
{ |
|||
var connection = new SqliteConnection(ConnectionString); |
|||
//connection.Open();
|
|||
|
|||
var options = new DbContextOptionsBuilder<AbpProDbContext>() |
|||
.UseSqlite(connection) |
|||
.Options; |
|||
|
|||
using (var context = new AbpProDbContext(options)) |
|||
{ |
|||
foreach (var entityType in context.Model.GetEntityTypes()) |
|||
{ |
|||
freeSql.CodeFirst.SyncStructure(entityType.ClrType, entityType.GetTableName(), true); |
|||
} |
|||
|
|||
//context.GetService<IRelationalDatabaseCreator>().CreateTables();
|
|||
} |
|||
|
|||
return connection; |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
|
|||
<IsPackable>false</IsPackable> |
|||
|
|||
<RootNamespace>Lion.AbpPro.FreeSqlRepository.Tests</RootNamespace> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" /> |
|||
<PackageReference Include="FreeSql.Provider.Sqlite" Version="$(FreesqlVersion)" /> |
|||
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Version="$(AbpPackageVersion)" /> |
|||
<PackageReference Include="Zack.EFCore.Batch.Sqlite_NET6" Version="$(ZackEFCoreBatchVersion)" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Lion.AbpPro.FreeSqlRepository\Lion.AbpPro.FreeSqlRepository.csproj" /> |
|||
<ProjectReference Include="..\Lion.AbpPro.EntityFrameworkCore.Tests\Lion.AbpPro.EntityFrameworkCore.Tests.csproj" /> |
|||
<ProjectReference Include="..\Lion.AbpPro.TestBase\Lion.AbpPro.TestBase.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,26 @@ |
|||
using System.Threading.Tasks; |
|||
using Lion.AbpPro.Users; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlRepository.Tests.Users |
|||
{ |
|||
|
|||
|
|||
public class UserFreeSqlBasicRepository_Tests: AbpProFreeSqlRepositoryTestBase |
|||
{ |
|||
private readonly IUserFreeSqlBasicRepository _userFreeSqlBasicRepository; |
|||
public UserFreeSqlBasicRepository_Tests() |
|||
{ |
|||
_userFreeSqlBasicRepository = GetRequiredService<IUserFreeSqlBasicRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_NotThrow_ListAsyncTest() |
|||
{ |
|||
var result = await _userFreeSqlBasicRepository.GetListAsync(); |
|||
result.ShouldNotBeNull(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using Lion.AbpPro.FreeSqlReppsitory.Tests; |
|||
using Lion.AbpPro.Localization; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Lion.AbpPro |
|||
{ |
|||
public abstract class AbpProFreeSqlRepositoryTestBase: AbpProTestBase<AbpProFreeSqlRepositoryTestModule> |
|||
{ |
|||
public AbpProFreeSqlRepositoryTestBase() |
|||
{ |
|||
ServiceProvider.InitializeLocalization(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Lion.AbpPro.FreeSqlReppsitory.Tests; |
|||
|
|||
[DependsOn( |
|||
typeof(AbpProTestBaseModule), |
|||
typeof(AbpProFreeSqlModule) |
|||
)] |
|||
|
|||
public class AbpProFreeSqlRepositoryTestModule: AbpModule |
|||
{ |
|||
|
|||
//public override void ConfigureServices(ServiceConfigurationContext context)
|
|||
//{
|
|||
|
|||
|
|||
|
|||
// var configuration = context.Services.GetConfiguration();
|
|||
// var connectionString = configuration.GetConnectionString("Default");
|
|||
// var freeSql = new FreeSql.FreeSqlBuilder()
|
|||
// .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
|
|||
// .UseAutoSyncStructure(true)
|
|||
// .Build();
|
|||
|
|||
// context.Services.AddSingleton<IFreeSql>(freeSql);
|
|||
//}
|
|||
|
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<Import Project="..\..\..\common.props" /> |
|||
<PropertyGroup> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
<RootNamespace>Lion.AbpPro</RootNamespace> |
|||
<IsPackable>false</IsPackable> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Lion.AbpPro.FreeSqlReppsitory\Lion.AbpPro.FreeSqlReppsitory.csproj" /> |
|||
<ProjectReference Include="..\Lion.AbpPro.TestBase\Lion.AbpPro.TestBase.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,29 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Lion.AbpPro.Users |
|||
{ |
|||
|
|||
|
|||
public class UserFreeSqlBasicRepository_Tests: AbpProFreeSqlRepositoryTestBase |
|||
{ |
|||
//private readonly IUserFreeSqlBasicRepository _userFreeSqlBasicRepository;
|
|||
//public UserFreeSqlBasicRepositoryTest()
|
|||
//{
|
|||
// _userFreeSqlBasicRepository = GetRequiredService<IUserFreeSqlBasicRepository>();
|
|||
//}
|
|||
|
|||
[Fact] |
|||
public void Should_NotThrow_ListAsyncTest() |
|||
{ |
|||
//var result = await _userFreeSqlBasicRepository.GetListAsync();
|
|||
var s = 1; |
|||
} |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue