|
@@ -0,0 +1,115 @@
|
|
|
+using AspNetCoreRateLimit;
|
|
|
+using FreeSql;
|
|
|
+using FreeSql.Internal;
|
|
|
+using HiTeachCE.Helpers;
|
|
|
+using HiTeachCE.Models;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
+using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
+using Serilog;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.Linq;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using ToolGood.Words;
|
|
|
+
|
|
|
+namespace HiTeachCE.Context
|
|
|
+{
|
|
|
+ public static class FreeSQLExtension
|
|
|
+ {
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// FreeSql
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ public static void AddContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
+ {
|
|
|
+ IConfigurationSection configurationSection = configuration.GetSection("ConnectionStrings:MySql");
|
|
|
+ IFreeSql fsql = new FreeSqlBuilder()
|
|
|
+ .UseConnectionString(DataType.MySql, configurationSection.Value)
|
|
|
+ .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
|
|
|
+ .UseAutoSyncStructure(true)
|
|
|
+ .UseMonitorCommand(cmd =>
|
|
|
+ {
|
|
|
+ Trace.WriteLine(cmd.CommandText + ";");
|
|
|
+ }
|
|
|
+ )
|
|
|
+ .Build()
|
|
|
+ .SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = true);//联级保存功能开启(默认为关闭)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ fsql.Aop.CurdAfter += (s, e) =>
|
|
|
+ {
|
|
|
+ Log.Debug($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}: FullName:{e.EntityType.FullName}" +
|
|
|
+ $" ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");
|
|
|
+
|
|
|
+ if (e.ElapsedMilliseconds > 200)
|
|
|
+ {
|
|
|
+ //记录日志
|
|
|
+ //发送短信给负责人
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ //敏感词处理
|
|
|
+ if (configuration["AuditValue:Enable"].ToBoolean())
|
|
|
+ {
|
|
|
+ IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();
|
|
|
+
|
|
|
+ fsql.Aop.AuditValue += (s, e) =>
|
|
|
+ {
|
|
|
+ if (e.Column.CsType == typeof(string) && e.Value != null)
|
|
|
+ {
|
|
|
+ string oldVal = (string)e.Value;
|
|
|
+ string newVal = illegalWords.Replace(oldVal);
|
|
|
+ //第二种处理敏感词的方式
|
|
|
+ //string newVal = oldVal.ReplaceStopWords();
|
|
|
+ if (newVal != oldVal)
|
|
|
+ {
|
|
|
+ e.Value = newVal;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ services.AddSingleton(fsql);
|
|
|
+ services.AddScoped<UnitOfWorkManager>();
|
|
|
+ fsql.GlobalFilter.Apply<IDeleteAduitEntity>("IsDeleted", a => a.IsDeleted == false);
|
|
|
+ //在运行时直接生成表结构
|
|
|
+ fsql.CodeFirst.SyncStructure(ReflexHelper.GetEntityTypes(typeof(IEntity)));
|
|
|
+ services.AddFreeRepository();
|
|
|
+ }
|
|
|
+ public static IServiceCollection AddFreeRepository(this IServiceCollection services)
|
|
|
+ {
|
|
|
+ services.TryAddScoped(typeof(IBaseRepository<>), typeof(GuidRepository<>));
|
|
|
+ services.TryAddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
|
|
|
+ services.TryAddScoped(typeof(IBaseRepository<,>), typeof(DefaultRepository<,>));
|
|
|
+ services.TryAddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
|
|
|
+ return services;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 配置限流依赖的服务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <param name="configuration"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection AddIpRateLimiting(this IServiceCollection services, IConfiguration configuration)
|
|
|
+ {
|
|
|
+ //加载配置
|
|
|
+ services.AddOptions();
|
|
|
+ //从IpRateLimiting.json获取相应配置
|
|
|
+ services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
|
|
|
+ services.Configure<IpRateLimitPolicies>(configuration.GetSection("IpRateLimitPolicies"));
|
|
|
+ //注入计数器和规则存储
|
|
|
+ services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();
|
|
|
+ services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
|
|
|
+
|
|
|
+ //配置(计数器密钥生成器)
|
|
|
+ services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
|
|
+
|
|
|
+ return services;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|