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

159 lines
6.1 KiB

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