平安校园
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.
 
 
 
 
 
 

141 line
5.2 KiB

  1. using MoYu.DependencyInjection;
  2. using SafeCampus.Application.Manager.DeepelephManager;
  3. using SafeCampus.Web.Core.Hub;
  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. services.AddSignalR();
  41. }
  42. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  43. {
  44. //var isDevelopment = App.HostEnvironment.IsDevelopment();
  45. //var isProduction = App.HostEnvironment.IsProduction();
  46. if (env.IsProduction())
  47. {
  48. //启动执行消息订阅
  49. Scoped.Create((_, scope) =>
  50. {
  51. var deepeleph = scope.ServiceProvider.GetRequiredService<IDeepelephManager>();
  52. deepeleph.SubscribeAlarm();
  53. //deepeleph.SubscribersRoomCall();
  54. deepeleph.SubscriberAttendance();
  55. });
  56. }
  57. //启动Web设置Configure组件
  58. app.UseComponent<WebSettingsApplicationComponent>(env);
  59. if (env.IsDevelopment())
  60. {
  61. app.UseDeveloperExceptionPage();
  62. }
  63. else
  64. {
  65. //启用gzip压缩
  66. app.UseResponseCompression();
  67. }
  68. // 启用HTTPS
  69. app.UseHttpsRedirection();
  70. // 添加状态码拦截中间件
  71. app.UseUnifyResultStatusCodes();
  72. app.UseStaticFiles();//静态文件访问配置
  73. //静态文件访问配置,暂时不开启
  74. // app.UseStaticFiles(new StaticFileOptions
  75. // {
  76. // ServeUnknownFileTypes = true,
  77. // FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),//wwwroot相当于真实目录
  78. // OnPrepareResponse = c =>
  79. // {
  80. // c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  81. // c.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800");
  82. // },
  83. // RequestPath = new PathString("/files")//src相当于别名,为了安全
  84. // });
  85. app.UseRouting();//路由配置
  86. app.UseCorsAccessor();//跨域配置
  87. app.UseWebStatus();//网站开启状态
  88. app.UseAuthentication();//认证
  89. app.UseAuthorization();//授权
  90. app.UseInject(string.Empty);
  91. app.UseForwardedHeaders();//Nginx代理的话获取真实IP
  92. app.UseEndpoints(endpoints =>
  93. {
  94. // 注册集线器
  95. endpoints.MapHubs();
  96. endpoints.MapControllers();
  97. });
  98. var path = Path.Combine(Directory.GetCurrentDirectory(), "Files");
  99. if (!Directory.Exists(path))
  100. {
  101. Directory.CreateDirectory(path);
  102. }
  103. var alarmImg = Path.Combine(Directory.GetCurrentDirectory(), "Files","alarmImg");
  104. if (!Directory.Exists(alarmImg))
  105. {
  106. Directory.CreateDirectory(alarmImg);
  107. }
  108. var personImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", "personImg");
  109. if (!Directory.Exists(personImg))
  110. {
  111. Directory.CreateDirectory(personImg);
  112. }
  113. var roomCallImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", "roomCallImg");
  114. if (!Directory.Exists(roomCallImg))
  115. {
  116. Directory.CreateDirectory(roomCallImg);
  117. }
  118. var attendanceImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", "attendanceImg");
  119. if (!Directory.Exists(attendanceImg))
  120. {
  121. Directory.CreateDirectory(attendanceImg);
  122. }
  123. app.UseFileServer(new FileServerOptions()
  124. {
  125. FileProvider = new PhysicalFileProvider(path),
  126. RequestPath = new PathString("/Files"),
  127. EnableDirectoryBrowsing = true
  128. });
  129. }
  130. }