Startup.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Admin.NET.Core;
  2. using AspNetCoreRateLimit;
  3. using Furion;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.Extensions.Logging;
  9. using NETCore.MailKit.Extensions;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Serialization;
  12. using OnceMi.AspNetCore.OSS;
  13. using System;
  14. using Yitter.IdGenerator;
  15. namespace Admin.NET.Web.Core;
  16. public class Startup : AppStartup
  17. {
  18. public void ConfigureServices(IServiceCollection services)
  19. {
  20. // 配置选项
  21. services.AddProjectOptions();
  22. // ORM-SqlSugar
  23. services.AddSqlSugarSetup();
  24. // JWT
  25. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
  26. // 允许跨域
  27. services.AddCorsAccessor();
  28. // 远程请求
  29. services.AddRemoteRequest();
  30. // 任务调度
  31. services.AddTaskScheduler();
  32. // 脱敏检测
  33. services.AddSensitiveDetection();
  34. // 结果拦截器
  35. services.AddMvcFilter<ResultFilter>();
  36. // 日志监听特性(拦截器)
  37. services.AddMonitorLogging();
  38. services.AddControllersWithViews()
  39. .AddAppLocalization()
  40. .AddNewtonsoftJson(options =>
  41. {
  42. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // 响应驼峰命名
  43. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
  44. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
  45. // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  46. })
  47. .AddInjectWithUnifyResult<AdminResultProvider>();
  48. // 限流服务
  49. services.AddInMemoryRateLimiting();
  50. services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
  51. // 事件总线
  52. services.AddEventBus(builder =>
  53. {
  54. builder.AddSubscriber<LogEventSubscriber>();
  55. });
  56. // OSS对象存储
  57. services.AddOSSService(options =>
  58. {
  59. options = App.GetOptions<OSSProviderOptions>();
  60. });
  61. // 电子邮件
  62. services.AddMailKit(options =>
  63. {
  64. options.UseMailKit(App.GetOptions<EmailOptions>());
  65. });
  66. // Redis缓存
  67. services.AddCSRedisSetup();
  68. // 模板引擎
  69. services.AddViewEngine();
  70. // 即时通讯
  71. services.AddSignalR();
  72. // logo显示
  73. services.AddLogoDisplay();
  74. // 日志记录
  75. services.AddLogging(builder =>
  76. {
  77. // 每天创建一个日志文件(消息日志、错误日志、警告日志)
  78. builder.AddFile("logs/{0:yyyyMMdd}_inf.log", options =>
  79. {
  80. options.WriteFilter = (logMsg) =>
  81. {
  82. return logMsg.LogLevel == LogLevel.Information;
  83. };
  84. options.FileNameRule = fileName =>
  85. {
  86. return string.Format(fileName, DateTime.Now);
  87. };
  88. options.FileSizeLimitBytes = 10 * 1024;
  89. options.MaxRollingFiles = 30;
  90. });
  91. builder.AddFile("logs/{0:yyyyMMdd}_err.log", options =>
  92. {
  93. options.WriteFilter = (logMsg) =>
  94. {
  95. return logMsg.LogLevel == LogLevel.Error;
  96. };
  97. options.FileNameRule = fileName =>
  98. {
  99. return string.Format(fileName, DateTime.Now);
  100. };
  101. options.FileSizeLimitBytes = 10 * 1024;
  102. options.MaxRollingFiles = 30;
  103. });
  104. builder.AddFile("logs/{0:yyyyMMdd}_wrn.log", options =>
  105. {
  106. options.WriteFilter = (logMsg) =>
  107. {
  108. return logMsg.LogLevel == LogLevel.Warning;
  109. };
  110. options.FileNameRule = fileName =>
  111. {
  112. return string.Format(fileName, DateTime.Now);
  113. };
  114. options.FileSizeLimitBytes = 10 * 1024;
  115. options.MaxRollingFiles = 30;
  116. });
  117. // 日志写入数据库
  118. builder.AddDatabase<DbLoggingWriter>(options =>
  119. {
  120. options.MinimumLevel = LogLevel.Information;
  121. });
  122. });
  123. // 设置雪花Id算法机器码
  124. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  125. {
  126. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  127. });
  128. }
  129. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  130. {
  131. if (env.IsDevelopment())
  132. {
  133. app.UseDeveloperExceptionPage();
  134. }
  135. else
  136. {
  137. app.UseExceptionHandler("/Home/Error");
  138. app.UseHsts();
  139. }
  140. // 添加状态码拦截中间件
  141. app.UseUnifyResultStatusCodes();
  142. // 配置多语言
  143. app.UseAppLocalization();
  144. // 启用HTTPS
  145. app.UseHttpsRedirection();
  146. app.UseStaticFiles();
  147. app.UseRouting();
  148. app.UseCorsAccessor();
  149. // 限流组件(在跨域之后)
  150. app.UseIpRateLimiting();
  151. app.UseClientRateLimiting();
  152. app.UseAuthentication();
  153. app.UseAuthorization();
  154. app.UseInject();
  155. app.UseEndpoints(endpoints =>
  156. {
  157. // 注册集线器
  158. endpoints.MapHubs();
  159. endpoints.MapControllerRoute(
  160. name: "default",
  161. pattern: "{controller=Home}/{action=Index}/{id?}");
  162. });
  163. }
  164. }