Startup.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Admin.NET.Core;
  2. using Furion;
  3. using Furion.Logging;
  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(App.Configuration);
  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<ActionFilter>();
  36. // 结果拦截器
  37. services.AddMvcFilter<ResultFilter>();
  38. // 日志监听特性(拦截器)
  39. services.AddMvcFilter<LoggingMonitorAttribute>();
  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.AddEventBus(builder =>
  52. {
  53. builder.AddSubscriber<LogEventSubscriber>();
  54. });
  55. // OSS对象存储
  56. services.AddOSSService(options =>
  57. {
  58. options = App.GetOptions<OSSProviderOptions>();
  59. });
  60. // 电子邮件
  61. services.AddMailKit(options =>
  62. {
  63. options.UseMailKit(App.GetOptions<EmailOptions>());
  64. });
  65. // Redis缓存
  66. services.AddCSRedisSetup();
  67. // 模板引擎
  68. services.AddViewEngine();
  69. // 即时通讯
  70. services.AddSignalR();
  71. // logo显示
  72. services.AddLogoDisplay();
  73. // 日志记录
  74. services.AddLogging(builder =>
  75. {
  76. // 错误级别日志归类
  77. builder.AddFile("logs/error.log", options =>
  78. {
  79. options.WriteFilter = (logMsg) =>
  80. {
  81. return logMsg.LogLevel == LogLevel.Error;
  82. };
  83. });
  84. // 每天创建一个日志文件
  85. builder.AddFile("logs/{0:yyyyMMdd}.log", options =>
  86. {
  87. options.FileNameRule = fileName =>
  88. {
  89. return string.Format(fileName, DateTime.UtcNow);
  90. };
  91. });
  92. // 日志写入数据库
  93. builder.AddDatabase<DbLoggingWriter>();
  94. });
  95. // 设置雪花Id算法机器码
  96. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  97. {
  98. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  99. });
  100. }
  101. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  102. {
  103. if (env.IsDevelopment())
  104. {
  105. app.UseDeveloperExceptionPage();
  106. }
  107. else
  108. {
  109. app.UseExceptionHandler("/Home/Error");
  110. app.UseHsts();
  111. }
  112. // 添加状态码拦截中间件
  113. app.UseUnifyResultStatusCodes();
  114. // 配置多语言
  115. app.UseAppLocalization();
  116. // 启用HTTPS
  117. app.UseHttpsRedirection();
  118. app.UseStaticFiles();
  119. app.UseRouting();
  120. app.UseCorsAccessor();
  121. app.UseAuthentication();
  122. app.UseAuthorization();
  123. app.UseInject();
  124. app.UseEndpoints(endpoints =>
  125. {
  126. // 注册集线器
  127. endpoints.MapHubs();
  128. endpoints.MapControllerRoute(
  129. name: "default",
  130. pattern: "{controller=Home}/{action=Index}/{id?}");
  131. });
  132. }
  133. }