Startup.cs 4.9 KB

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