平安校园
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

132 satır
4.5 KiB

  1. //
  2. using MoYu.DependencyInjection;
  3. using SafeCampus.Application.Manager.DeepelephManager;
  4. using SafeCampus.Core.Extension;
  5. namespace SafeCampus.Web.Core;
  6. /// <summary>
  7. /// Web启动项配置
  8. /// </summary>
  9. [AppStartup(99)]
  10. public class Startup : AppStartup
  11. {
  12. public void ConfigureServices(IServiceCollection services)
  13. {
  14. //启动LoggingMonitor操作日志写入数据库组件
  15. services.AddComponent<LoggingMonitorComponent>();
  16. //认证组件
  17. services.AddComponent<AuthComponent>();
  18. //启动Web设置ConfigureServices组件
  19. services.AddComponent<WebSettingsComponent>();
  20. //gip压缩
  21. services.AddComponent<GzipCompressionComponent>();
  22. //定时任务
  23. //services.AddSchedule();
  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. }).AddInjectWithUnifyResult<SafeCampusResultProvider>()//配置统一返回模型
  31. ;
  32. //Nginx代理的话获取真实IP
  33. services.Configure<ForwardedHeadersOptions>(options =>
  34. {
  35. options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  36. //新增如下两行
  37. options.KnownNetworks.Clear();
  38. options.KnownProxies.Clear();
  39. });
  40. services.AddRemoteRequest();
  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.SubscriberRoomCall();
  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. endpoints.MapControllers();
  95. });
  96. var path = Path.Combine(Directory.GetCurrentDirectory(), "Files");
  97. if (!Directory.Exists(path))
  98. {
  99. Directory.CreateDirectory(path);
  100. }
  101. var alarmImg = Path.Combine(Directory.GetCurrentDirectory(), "Files","alarmImg");
  102. if (!Directory.Exists(alarmImg))
  103. {
  104. Directory.CreateDirectory(alarmImg);
  105. }
  106. app.UseFileServer(new FileServerOptions()
  107. {
  108. FileProvider = new PhysicalFileProvider(path),
  109. RequestPath = new PathString("/Files"),
  110. EnableDirectoryBrowsing = true
  111. });
  112. }
  113. }