Startup.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Furion;
  2. using Furion.Schedule;
  3. using Furion.VirtualFileServer;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http.Features;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Hosting;
  9. using Serilog;
  10. using System.Text.Json;
  11. using System.Text.Json.Serialization;
  12. using YBEE.EQM.Application;
  13. using YBEE.EQM.Core;
  14. namespace YBEE.EQM.Web.Core;
  15. public class Startup : AppStartup
  16. {
  17. public void ConfigureServices(IServiceCollection services)
  18. {
  19. services.AddResponseCompression();
  20. services.AddConsoleFormatter();
  21. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
  22. services.AddConfigurableOptions<AuthOptions>();
  23. services.AddConfigurableOptions<RefreshTokenSettingOptions>();
  24. services.AddConfigurableOptions<EqmSiteOptions>();
  25. services.AddCorsAccessor();
  26. services.AddRemoteRequest();
  27. // 添加抽样执行服务
  28. services.AddSingleton<ExamSampleWorker>();
  29. services.AddControllers()
  30. .AddJsonOptions(options =>
  31. {
  32. // 日期时间类型格式化
  33. options.JsonSerializerOptions.Converters.AddDateTimeTypeConverters("yyyy-MM-dd HH:mm:ss");
  34. // 忽略循环引用
  35. options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
  36. // 包含成员字段序列化
  37. options.JsonSerializerOptions.IncludeFields = true;
  38. // 允许尾随逗号
  39. options.JsonSerializerOptions.AllowTrailingCommas = true;
  40. // 允许注释
  41. options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
  42. // long 转 string
  43. options.JsonSerializerOptions.Converters.AddLongTypeConverters();
  44. options.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
  45. options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  46. })
  47. .AddInjectWithUnifyResult<XnRestfulResultProvider>()
  48. .AddMvcFilter<RequestActionFilter>();
  49. services.Configure<FormOptions>(x =>
  50. {
  51. x.ValueLengthLimit = int.MaxValue;
  52. x.MultipartBoundaryLengthLimit = int.MaxValue;
  53. });
  54. //services.AddSwaggerGen(options =>
  55. //{
  56. // options.DescribeAllParametersInCamelCase();
  57. //});
  58. services.AddSnowflakeId(); // 雪花ID
  59. // 注册EventBus服务
  60. services.AddEventBus(builder =>
  61. {
  62. // 注册 Log 日志订阅者
  63. builder.AddSubscriber<LogEventSubscriber>();
  64. });
  65. //// 注册作业调度
  66. //services.AddSchedule(options =>
  67. //{
  68. // if (int.TryParse(App.Configuration["EqmSite:SyncQuestionnaireIntervalMinute"], out int m))
  69. // {
  70. // options.LogEnabled = false;
  71. // var triggerBuilder = Triggers.PeriodMinutes(m).SetRunOnStart(true).SetTriggerId($"patriarch_questionnaire_progress_sync_{m}m");
  72. // options.AddJob<ExamPatriarchQuestionnaireProgressSyncJob>("patriarch_questionnaire_progress_sync", concurrent: false, triggerBuilder);
  73. // }
  74. //});
  75. }
  76. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  77. {
  78. if (env.IsDevelopment())
  79. {
  80. app.UseDeveloperExceptionPage();
  81. }
  82. // 添加状态码拦截中间件,处理 401、403 等
  83. app.UseUnifyResultStatusCodes();
  84. if (!env.IsDevelopment())
  85. {
  86. //app.UseHttpsRedirection();
  87. }
  88. app.UseResponseCompression();
  89. app.UseStaticFiles(new StaticFileOptions
  90. {
  91. ContentTypeProvider = FS.GetFileExtensionContentTypeProvider()
  92. });
  93. app.UseScheduleUI(options =>
  94. {
  95. options.RequestPath = "/sync-jobs";
  96. });
  97. //// 如果目录不存在则创建
  98. //var option = App.GetOptions<EqmSiteOptions>();
  99. //var staticRoot = Path.Combine(option.ExamResultRoot, option.RequestPath); // 注意这里不要添加 /
  100. //if (!Directory.Exists(staticRoot)) Directory.CreateDirectory(staticRoot);
  101. //app.UseFileServer(new FileServerOptions
  102. //{
  103. // RequestPath = $"/{option.RequestPath}", // 配置访问地址,需以 / 开头,通常和目录名称一致,也可以不同
  104. // FileProvider = new PhysicalFileProvider(staticRoot),
  105. // EnableDirectoryBrowsing = false,
  106. //});
  107. // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
  108. app.UseSerilogRequestLogging();
  109. app.UseRouting();
  110. app.UseCorsAccessor();
  111. app.UseAuthentication();
  112. app.UseAuthorization();
  113. app.UseInject(string.Empty);
  114. app.UseEndpoints(endpoints =>
  115. {
  116. endpoints.MapControllers();
  117. endpoints.MapFallbackToFile("index.html");
  118. });
  119. }
  120. }