Startup.cs 6.1 KB

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