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

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 
  2. //
  3. using MoYu.DependencyInjection;
  4. using SafeCampus.Application.Manager.DeepelephManager;
  5. using SafeCampus.Core.Extension;
  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. //定时任务
  24. //services.AddSchedule();
  25. //添加控制器相关
  26. services.AddControllers().AddNewtonsoftJson(options => //配置json
  27. {
  28. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();// 首字母小写(驼峰样式)
  29. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";// 时间格式化
  30. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;// 忽略循环引用
  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. services.AddRemoteRequest();
  42. }
  43. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  44. {
  45. //var isDevelopment = App.HostEnvironment.IsDevelopment();
  46. //var isProduction = App.HostEnvironment.IsProduction();
  47. if (env.IsProduction())
  48. {
  49. //启动执行消息订阅
  50. Scoped.Create((_, scope) =>
  51. {
  52. var deepeleph = scope.ServiceProvider.GetRequiredService<IDeepelephManager>();
  53. deepeleph.SubscribeAlarm();
  54. deepeleph.SubscriberRoomCall();
  55. //deepeleph.SubscriberAttendance();
  56. });
  57. }
  58. //启动Web设置Configure组件
  59. app.UseComponent<WebSettingsApplicationComponent>(env);
  60. if (env.IsDevelopment())
  61. {
  62. app.UseDeveloperExceptionPage();
  63. }
  64. else
  65. {
  66. //启用gzip压缩
  67. app.UseResponseCompression();
  68. }
  69. // 启用HTTPS
  70. app.UseHttpsRedirection();
  71. // 添加状态码拦截中间件
  72. app.UseUnifyResultStatusCodes();
  73. app.UseStaticFiles();//静态文件访问配置
  74. //静态文件访问配置,暂时不开启
  75. // app.UseStaticFiles(new StaticFileOptions
  76. // {
  77. // ServeUnknownFileTypes = true,
  78. // FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),//wwwroot相当于真实目录
  79. // OnPrepareResponse = c =>
  80. // {
  81. // c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  82. // c.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800");
  83. // },
  84. // RequestPath = new PathString("/files")//src相当于别名,为了安全
  85. // });
  86. app.UseRouting();//路由配置
  87. app.UseCorsAccessor();//跨域配置
  88. app.UseWebStatus();//网站开启状态
  89. app.UseAuthentication();//认证
  90. app.UseAuthorization();//授权
  91. app.UseInject(string.Empty);
  92. app.UseForwardedHeaders();//Nginx代理的话获取真实IP
  93. app.UseEndpoints(endpoints =>
  94. {
  95. endpoints.MapControllers();
  96. });
  97. var path = Path.Combine(Directory.GetCurrentDirectory(), "Files");
  98. if (!Directory.Exists(path))
  99. {
  100. Directory.CreateDirectory(path);
  101. }
  102. var alarmImg = Path.Combine(Directory.GetCurrentDirectory(), "Files","alarmImg");
  103. if (!Directory.Exists(alarmImg))
  104. {
  105. Directory.CreateDirectory(alarmImg);
  106. }
  107. app.UseFileServer(new FileServerOptions()
  108. {
  109. FileProvider = new PhysicalFileProvider(path),
  110. RequestPath = new PathString("/Files"),
  111. EnableDirectoryBrowsing = true
  112. });
  113. }
  114. }