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

132 line
4.9 KiB

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