Program.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.Extensions.DependencyInjection.Extensions;
  4. using Microsoft.OpenApi.Models;
  5. using System.Security.Cryptography.Xml;
  6. using Microsoft.Extensions.Diagnostics.HealthChecks;
  7. using Microsoft.AspNetCore.Diagnostics.HealthChecks;
  8. var builder = WebApplication.CreateBuilder(args);
  9. builder.Services.Init();
  10. builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  11. builder.Services.AddControllers(option =>
  12. {
  13. option.Filters.Add(typeof(ResponseExceptionFilterAttribute));
  14. option.Filters.Add(typeof(MyAuthorizationFactory.MyActionFilter));
  15. });
  16. //builder.Services.AddHostedService<TaskWorker>();
  17. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  18. builder.Services.AddEndpointsApiExplorer();
  19. builder.Services.AddSwaggerGen(option =>
  20. {
  21. option.AddSecurityDefinition("Authorization", new OpenApiSecurityScheme()
  22. {
  23. Description = "请输入 token",
  24. Name = "Authorization",
  25. In = ParameterLocation.Header,
  26. Type = SecuritySchemeType.ApiKey
  27. });
  28. option.AddSecurityRequirement(new OpenApiSecurityRequirement
  29. {
  30. {
  31. new OpenApiSecurityScheme
  32. {
  33. Reference = new OpenApiReference()
  34. {
  35. Type = ReferenceType.SecurityScheme,
  36. Id = "Bearer"
  37. }
  38. }, Array.Empty<string>()
  39. }
  40. });
  41. });
  42. builder.Services.AddCors(options =>
  43. {
  44. options.AddPolicy("AllowAll",
  45. builder =>
  46. {
  47. builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
  48. });
  49. });
  50. // 添加健康检查服务
  51. builder.Services.AddHealthChecks().AddCheck("self", () => HealthCheckResult.Healthy());
  52. var app = builder.Build();
  53. app.MapHealthChecks("/health", new HealthCheckOptions
  54. {
  55. ResponseWriter = async (context, report) =>
  56. {
  57. context.Response.ContentType = "application/json";
  58. await context.Response.WriteAsJsonAsync(new
  59. {
  60. status = report.Status.ToString(),
  61. checks = report.Entries.Select(e => new
  62. {
  63. name = e.Key,
  64. status = e.Value.Status.ToString(),
  65. description = e.Value.Description
  66. })
  67. });
  68. }
  69. });
  70. // Configure the HTTP request pipeline.
  71. if (app.Environment.IsDevelopment())
  72. {
  73. app.UseSwagger();
  74. app.UseSwaggerUI();
  75. }
  76. //app.UseSwagger();
  77. //app.UseSwaggerUI();
  78. app.UseRequestLocalization();
  79. // 添加下面这行
  80. System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
  81. app.UseHttpsRedirection();
  82. app.UseAuthorization();
  83. app.MapControllers();
  84. app.UseCors("AllowAll"); // 启用 CORS 中间件
  85. app.Run();