|
|
|
@ -7,8 +7,10 @@ using Infrastructure.Options;
|
|
|
|
|
using Infrastructure.Repository;
|
|
|
|
|
using Infrastructure.Repository.Mongo;
|
|
|
|
|
using Infrastructure.Repository.Redis;
|
|
|
|
|
using Infrastructure.Seed;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
@ -24,6 +26,29 @@ namespace Infrastructure.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
public static WebApplicationBuilder AddDefaultInfrastructure<T>(this WebApplicationBuilder builder)
|
|
|
|
|
where T : IEquatable<T>
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(builder);
|
|
|
|
|
var services = builder.Services;
|
|
|
|
|
var configuration = builder.Configuration;
|
|
|
|
|
services.AddDefaultUserContext<T>()
|
|
|
|
|
.AddDefaultAesEncryption()
|
|
|
|
|
.AddDefaultAuthentication(configuration)
|
|
|
|
|
.AddDefaultAuthorize(configuration)
|
|
|
|
|
.AddDefaultControllers()
|
|
|
|
|
.AddDefaultCors(configuration)
|
|
|
|
|
.AddDefaultApiVersioning(configuration)
|
|
|
|
|
.AddDefaultRedis(configuration)
|
|
|
|
|
.AddDefaultSerilog(configuration)
|
|
|
|
|
.AddDefaultRepositoryContext()
|
|
|
|
|
.AddDefaultSqlSugar(configuration, builder.Environment)
|
|
|
|
|
.AddDefaultMongoDb(configuration)
|
|
|
|
|
.AddDefaultTokenContext()
|
|
|
|
|
.AddDefaultDatabaseSeed();
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static TOptions? GetOptions<TOptions>(this IConfiguration configuration, string sectionName)
|
|
|
|
|
where TOptions : OptionsBase
|
|
|
|
|
{
|
|
|
|
@ -222,7 +247,7 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddAesEncryption(this IServiceCollection services)
|
|
|
|
|
public static IServiceCollection AddDefaultAesEncryption(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
services.TryAddSingleton<IEncryptionService, EncryptionService>();
|
|
|
|
@ -235,7 +260,7 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddMongoDbSetup(
|
|
|
|
|
public static IServiceCollection AddDefaultMongoDb(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
@ -276,10 +301,13 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
/// <param name="hostEnvironment"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddDefaultSqlSugarSetup(
|
|
|
|
|
public static IServiceCollection AddDefaultSqlSugar(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
IWebHostEnvironment hostEnvironment)
|
|
|
|
|
IWebHostEnvironment hostEnvironment,
|
|
|
|
|
Action<object, DataFilterModel>? onDataChanging = null,
|
|
|
|
|
Action<DiffLogModel>? onDiffLogEvent = null
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
|
|
|
|
@ -330,13 +358,20 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sugarScope = new SqlSugarScope(connectionConfig, config =>
|
|
|
|
|
var sugarScope = new SqlSugarScope(connectionConfig, client =>
|
|
|
|
|
{
|
|
|
|
|
config.QueryFilter.AddTableFilter<IDeletable>(d => !d.IsDeleted);
|
|
|
|
|
client.QueryFilter.AddTableFilter<IDeletable>(d => !d.IsDeleted);
|
|
|
|
|
if (hostEnvironment.IsDevelopment() || hostEnvironment.IsStaging())
|
|
|
|
|
{
|
|
|
|
|
config.Aop.OnLogExecuting = (sql, parameters) => { Log.Logger.Information(sql); };
|
|
|
|
|
client.Aop.OnLogExecuted = (sql, parameters) =>
|
|
|
|
|
{
|
|
|
|
|
var elapsed = client.Ado.SqlExecutionTime.TotalSeconds;
|
|
|
|
|
Log.Logger.Information("sql: {sql} elapsed: {time} seconds", sql, elapsed);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.Aop.DataExecuting = onDataChanging;
|
|
|
|
|
client.Aop.OnDiffLogEvent = onDiffLogEvent;
|
|
|
|
|
});
|
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sugarScope);
|
|
|
|
|
return services;
|
|
|
|
@ -466,6 +501,12 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置Serilog
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddDefaultSerilog(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
@ -481,8 +522,7 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
var loggerConfiguration = new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Information()
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
|
|
|
|
|
.Enrich.FromLogContext();
|
|
|
|
|
.WriteTo.Console(theme: AnsiConsoleTheme.Code);
|
|
|
|
|
|
|
|
|
|
if (serilogOptions.WriteFile)
|
|
|
|
|
{
|
|
|
|
@ -507,4 +547,13 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
});
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddDefaultDatabaseSeed(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
|
|
|
|
|
services.TryAddScoped<DatabaseContext>();
|
|
|
|
|
services.TryAddScoped<DatabaseSeed>();
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|