平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Startup.cs 4.9 KiB

3 maanden geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 
  2. using MoYu.DependencyInjection;
  3. using SafeCampus.Application.Manager.DeepelephManager;
  4. namespace SafeCampus.Web.Core;
  5. /// <summary>
  6. /// Web启动项配置
  7. /// </summary>
  8. [AppStartup(99)]
  9. public class Startup : AppStartup
  10. {
  11. public void ConfigureServices(IServiceCollection services)
  12. {
  13. //启动LoggingMonitor操作日志写入数据库组件
  14. services.AddComponent<LoggingMonitorComponent>();
  15. //认证组件
  16. services.AddComponent<AuthComponent>();
  17. //启动Web设置ConfigureServices组件
  18. services.AddComponent<WebSettingsComponent>();
  19. //gip压缩
  20. services.AddComponent<GzipCompressionComponent>();
  21. //定时任务
  22. //services.AddSchedule();
  23. //添加控制器相关
  24. services.AddControllers().AddNewtonsoftJson(options => //配置json
  25. {
  26. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();// 首字母小写(驼峰样式)
  27. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";// 时间格式化
  28. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;// 忽略循环引用
  29. }).AddInjectWithUnifyResult<SafeCampusResultProvider>()//配置统一返回模型
  30. ;
  31. //Nginx代理的话获取真实IP
  32. services.Configure<ForwardedHeadersOptions>(options =>
  33. {
  34. options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  35. //新增如下两行
  36. options.KnownNetworks.Clear();
  37. options.KnownProxies.Clear();
  38. });
  39. services.AddRemoteRequest();
  40. }
  41. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  42. {
  43. //var isDevelopment = App.HostEnvironment.IsDevelopment();
  44. //var isProduction = App.HostEnvironment.IsProduction();
  45. if (env.IsProduction())
  46. {
  47. //启动执行消息订阅
  48. Scoped.Create((_, scope) =>
  49. {
  50. var deepeleph = scope.ServiceProvider.GetRequiredService<IDeepelephManager>();
  51. deepeleph.SubscribeAlarm();
  52. //deepeleph.SubscribersRoomCall();
  53. deepeleph.SubscriberAttendance();
  54. });
  55. }
  56. //启动Web设置Configure组件
  57. app.UseComponent<WebSettingsApplicationComponent>(env);
  58. if (env.IsDevelopment())
  59. {
  60. app.UseDeveloperExceptionPage();
  61. }
  62. else
  63. {
  64. //启用gzip压缩
  65. app.UseResponseCompression();
  66. }
  67. // 启用HTTPS
  68. app.UseHttpsRedirection();
  69. // 添加状态码拦截中间件
  70. app.UseUnifyResultStatusCodes();
  71. app.UseStaticFiles();//静态文件访问配置
  72. //静态文件访问配置,暂时不开启
  73. // app.UseStaticFiles(new StaticFileOptions
  74. // {
  75. // ServeUnknownFileTypes = true,
  76. // FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),//wwwroot相当于真实目录
  77. // OnPrepareResponse = c =>
  78. // {
  79. // c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  80. // c.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800");
  81. // },
  82. // RequestPath = new PathString("/files")//src相当于别名,为了安全
  83. // });
  84. app.UseRouting();//路由配置
  85. app.UseCorsAccessor();//跨域配置
  86. app.UseWebStatus();//网站开启状态
  87. app.UseAuthentication();//认证
  88. app.UseAuthorization();//授权
  89. app.UseInject(string.Empty);
  90. app.UseForwardedHeaders();//Nginx代理的话获取真实IP
  91. app.UseEndpoints(endpoints =>
  92. {
  93. endpoints.MapControllers();
  94. });
  95. var path = Path.Combine(Directory.GetCurrentDirectory(), "Files");
  96. if (!Directory.Exists(path))
  97. {
  98. Directory.CreateDirectory(path);
  99. }
  100. var alarmImg = Path.Combine(Directory.GetCurrentDirectory(), "Files","alarmImg");
  101. if (!Directory.Exists(alarmImg))
  102. {
  103. Directory.CreateDirectory(alarmImg);
  104. }
  105. var roomCallImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", "roomCallImg");
  106. if (!Directory.Exists(roomCallImg))
  107. {
  108. Directory.CreateDirectory(roomCallImg);
  109. }
  110. var attendanceImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", "attendanceImg");
  111. if (!Directory.Exists(attendanceImg))
  112. {
  113. Directory.CreateDirectory(attendanceImg);
  114. }
  115. app.UseFileServer(new FileServerOptions()
  116. {
  117. FileProvider = new PhysicalFileProvider(path),
  118. RequestPath = new PathString("/Files"),
  119. EnableDirectoryBrowsing = true
  120. });
  121. }
  122. }