Startup.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Admin.NET.Core;
  2. using Admin.NET.Core.Service;
  3. using Furion;
  4. using Furion.FriendlyException;
  5. using Furion.Logging.Extensions;
  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 NETCore.MailKit.Infrastructure.Internal;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Serialization;
  15. using OnceMi.AspNetCore.OSS;
  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
  25. services.AddSqlSugarSetup(App.Configuration);
  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. services.AddControllersWithViews()
  37. .AddMvcFilter<ActionFilter>()
  38. .AddMvcFilter<ResultFilter>()
  39. .AddNewtonsoftJson(options =>
  40. {
  41. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // 响应驼峰命名
  42. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
  43. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
  44. // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  45. })
  46. .AddInjectWithUnifyResult<AdminResultProvider>();
  47. // 注册事件总线
  48. services.AddEventBus(builder =>
  49. {
  50. builder.AddSubscriber<LogEventSubscriber>();
  51. });
  52. // 注册OSS对象存储
  53. services.AddOSSService(options =>
  54. {
  55. options = App.GetOptions<OSSProviderOptions>();
  56. });
  57. // 注册邮件
  58. services.AddMailKit(options =>
  59. {
  60. options.UseMailKit(App.GetOptions<EmailOptions>());
  61. });
  62. // 注册Redis缓存
  63. services.AddCSRedisSetup();
  64. // 注册模板引擎
  65. services.AddViewEngine();
  66. // 注册即时通讯
  67. services.AddSignalR();
  68. // 注册logo显示
  69. services.AddLogoDisplay();
  70. // 注册日志
  71. services.AddFileLogging();
  72. services.AddFileLogging("logs/error.log", options =>
  73. {
  74. options.WriteFilter = (logMsg) =>
  75. {
  76. return logMsg.LogLevel == LogLevel.Error;
  77. };
  78. });
  79. }
  80. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  81. {
  82. if (env.IsDevelopment())
  83. {
  84. app.UseDeveloperExceptionPage();
  85. }
  86. else
  87. {
  88. app.UseExceptionHandler("/Home/Error");
  89. app.UseHsts();
  90. }
  91. // 添加状态码拦截中间件
  92. app.UseUnifyResultStatusCodes();
  93. // 启用HTTPS
  94. app.UseHttpsRedirection();
  95. app.UseStaticFiles();
  96. // HTTP请求日志
  97. app.UseHttpLogging();
  98. app.UseRouting();
  99. app.UseCorsAccessor();
  100. app.UseAuthentication();
  101. app.UseAuthorization();
  102. app.UseInject();
  103. app.UseEndpoints(endpoints =>
  104. {
  105. // 注册集线器
  106. endpoints.MapHubs();
  107. endpoints.MapControllerRoute(
  108. name: "default",
  109. pattern: "{controller=Home}/{action=Index}/{id?}");
  110. });
  111. // 设置雪花Id算法机器码
  112. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  113. {
  114. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  115. });
  116. }
  117. }