| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using molilian.core;
- using dodohold.core;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using Microsoft.OpenApi.Models;
- using System.Security.Cryptography.Xml;
- using Microsoft.Extensions.Diagnostics.HealthChecks;
- using Microsoft.AspNetCore.Diagnostics.HealthChecks;
- var builder = WebApplication.CreateBuilder(args);
- builder.Services.Init();
- builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- builder.Services.AddControllers(option =>
- {
- option.Filters.Add(typeof(ResponseExceptionFilterAttribute));
- option.Filters.Add(typeof(MyAuthorizationFactory.MyActionFilter));
- });
- //builder.Services.AddHostedService<TaskWorker>();
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
- builder.Services.AddEndpointsApiExplorer();
- builder.Services.AddSwaggerGen(option =>
- {
- option.AddSecurityDefinition("Authorization", new OpenApiSecurityScheme()
- {
- Description = "请输入 token",
- Name = "Authorization",
- In = ParameterLocation.Header,
- Type = SecuritySchemeType.ApiKey
- });
- option.AddSecurityRequirement(new OpenApiSecurityRequirement
- {
- {
- new OpenApiSecurityScheme
- {
- Reference = new OpenApiReference()
- {
- Type = ReferenceType.SecurityScheme,
- Id = "Bearer"
- }
- }, Array.Empty<string>()
- }
- });
- });
- builder.Services.AddCors(options =>
- {
- options.AddPolicy("AllowAll",
- builder =>
- {
- builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
- });
- });
- // 添加健康检查服务
- builder.Services.AddHealthChecks().AddCheck("self", () => HealthCheckResult.Healthy());
- var app = builder.Build();
- app.MapHealthChecks("/health", new HealthCheckOptions
- {
- ResponseWriter = async (context, report) =>
- {
- context.Response.ContentType = "application/json";
- await context.Response.WriteAsJsonAsync(new
- {
- status = report.Status.ToString(),
- checks = report.Entries.Select(e => new
- {
- name = e.Key,
- status = e.Value.Status.ToString(),
- description = e.Value.Description
- })
- });
- }
- });
- // Configure the HTTP request pipeline.
- if (app.Environment.IsDevelopment())
- {
- app.UseSwagger();
- app.UseSwaggerUI();
- }
- //app.UseSwagger();
- //app.UseSwaggerUI();
- app.UseRequestLocalization();
- // 添加下面这行
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- app.UseHttpsRedirection();
- app.UseAuthorization();
- app.MapControllers();
- app.UseCors("AllowAll"); // 启用 CORS 中间件
- app.Run();
|