Startup.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // 订阅日志事件
  57. builder.AddSubscriber<LogEventSubscriber>();
  58. // 事件执行器(失败重试)
  59. builder.AddExecutor<RetryEventHandlerExecutor>();
  60. });
  61. // OSS对象存储(必须一个个赋值)
  62. var opt = App.GetOptions<OSSProviderOptions>();
  63. services.AddOSSService(opt.ProviderName, options =>
  64. {
  65. options.Provider = opt.Provider;
  66. options.Endpoint = opt.Endpoint;
  67. options.AccessKey = opt.AccessKey;
  68. options.SecretKey = opt.SecretKey;
  69. options.Region = opt.Region;
  70. options.IsEnableCache = opt.IsEnableCache;
  71. options.IsEnableHttps = opt.IsEnableHttps;
  72. });
  73. // 电子邮件
  74. services.AddMailKit(options =>
  75. {
  76. options.UseMailKit(App.GetOptions<EmailOptions>());
  77. });
  78. // Redis缓存
  79. services.AddCSRedisSetup();
  80. // 模板引擎
  81. services.AddViewEngine();
  82. // 即时通讯
  83. services.AddSignalR();
  84. // logo显示
  85. services.AddLogoDisplay();
  86. // 日志记录
  87. services.AddLogging(builder =>
  88. {
  89. // 每天创建一个日志文件(消息日志、错误日志、警告日志)
  90. builder.AddFile("logs/{0:yyyyMMdd}_inf.log", options =>
  91. {
  92. options.WriteFilter = (logMsg) =>
  93. {
  94. return logMsg.LogLevel == LogLevel.Information;
  95. };
  96. options.FileNameRule = fileName =>
  97. {
  98. return string.Format(fileName, DateTime.Now);
  99. };
  100. options.FileSizeLimitBytes = 10 * 1024;
  101. options.MaxRollingFiles = 30;
  102. });
  103. builder.AddFile("logs/{0:yyyyMMdd}_err.log", options =>
  104. {
  105. options.WriteFilter = (logMsg) =>
  106. {
  107. return logMsg.LogLevel == LogLevel.Error;
  108. };
  109. options.FileNameRule = fileName =>
  110. {
  111. return string.Format(fileName, DateTime.Now);
  112. };
  113. options.FileSizeLimitBytes = 10 * 1024;
  114. options.MaxRollingFiles = 30;
  115. });
  116. builder.AddFile("logs/{0:yyyyMMdd}_wrn.log", options =>
  117. {
  118. options.WriteFilter = (logMsg) =>
  119. {
  120. return logMsg.LogLevel == LogLevel.Warning;
  121. };
  122. options.FileNameRule = fileName =>
  123. {
  124. return string.Format(fileName, DateTime.Now);
  125. };
  126. options.FileSizeLimitBytes = 10 * 1024;
  127. options.MaxRollingFiles = 30;
  128. });
  129. // 日志写入数据库
  130. builder.AddDatabase<DbLoggingWriter>(options =>
  131. {
  132. options.MinimumLevel = LogLevel.Information;
  133. });
  134. });
  135. // 设置雪花Id算法机器码
  136. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  137. {
  138. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  139. });
  140. }
  141. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  142. {
  143. if (env.IsDevelopment())
  144. {
  145. app.UseDeveloperExceptionPage();
  146. }
  147. else
  148. {
  149. app.UseExceptionHandler("/Home/Error");
  150. app.UseHsts();
  151. }
  152. // 添加状态码拦截中间件
  153. app.UseUnifyResultStatusCodes();
  154. // 配置多语言
  155. app.UseAppLocalization();
  156. // 启用HTTPS
  157. app.UseHttpsRedirection();
  158. app.UseStaticFiles();
  159. app.UseRouting();
  160. app.UseCorsAccessor();
  161. // 限流组件(在跨域之后)
  162. app.UseIpRateLimiting();
  163. app.UseClientRateLimiting();
  164. app.UseAuthentication();
  165. app.UseAuthorization();
  166. // 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存)
  167. app.UseKnife4UI(options =>
  168. {
  169. options.RoutePrefix = "kapi";
  170. foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
  171. {
  172. options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
  173. }
  174. });
  175. app.UseInject();
  176. app.UseEndpoints(endpoints =>
  177. {
  178. // 注册集线器
  179. endpoints.MapHubs();
  180. endpoints.MapControllerRoute(
  181. name: "default",
  182. pattern: "{controller=Home}/{action=Index}/{id?}");
  183. });
  184. }
  185. }