Files
pfm/PostFundManagement.Api/Program.cs
T
hermes ab2ef9da31 Added healthchecks
Configured scheduler schema
Added dataprotection support
2026-08-21 11:19:06 +02:00

101 lines
3.5 KiB
C#

using Microsoft.Extensions.Diagnostics.HealthChecks;
using PostFundManagement.Api.Extensions;
using PostFundManagement.Domain.Extensions;
using PostFundManagement.Domain.Health;
using PostFundManagement.Domain.Mediator;
using static PostFundManagement.Domain.Extensions.Constants;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer()
.AddApiVersioning(options => options.ApiVersionReader = new HeaderApiVersionReader());
builder.Services.AddApiVersioning().AddApiExplorer();
builder.Services.AddEndpoints(Assembly.GetExecutingAssembly());
builder.Services.AddApiServices(builder.Configuration);
builder.Services.AddMediator();
builder.Services.AddSecurityApiSdk(builder.Configuration);
builder.Services.AddWebSecurity(builder.Configuration);
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>));
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>));
builder.Services.AddQuartzScheduler(DefaultSchedulerName, builder.Configuration);
builder.Services.AddEmailServices(builder.Configuration);
builder.Services.AddHttpClient();
builder.Services.AddHashServices(builder.Configuration);
builder.Services.AddDataProtectionDatabase(builder.Configuration);
builder.Services.AddApplicationDbContext(builder.Configuration);
builder.Services.AddHealthChecks()
.AddCheck<QuartzHealthCheck>("QuartzScheduler")
.AddCheck<PostgresHealthCheck>("PostgresDatabase")
.AddCheck("Self", () => HealthCheckResult.Healthy());
var app = builder.Build();
var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
var scheduler = await schedulerFactory.GetScheduler(DefaultSchedulerName);
if (!scheduler!.IsStarted)
await scheduler.Start();
app.UseHsts();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
ApiVersionSet versionSet = app.NewApiVersionSet("v1")
.HasApiVersion(new ApiVersion(1))
.HasApiVersion(new ApiVersion(2))
.ReportApiVersions()
.Build();
var versionGroups = new Dictionary<int, RouteGroupBuilder>
{
{ 1, app.MapGroup("v{version:apiVersion}").WithApiVersionSet(versionSet) }
};
app.MapEndpoints(versionGroups);
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = _ => true,
AllowCachingResponses = true,
ResponseWriter = HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse
});
app.MapHealthChecksUI(options => { options.UIPath = "/healthui"; });
app.UseHealthChecks("/ready");
app.MapOpenApi();
var apiVersions = app.DescribeApiVersions()
.OrderByDescending(o => o.ApiVersion.MajorVersion)
.ToList();
foreach (var description in app.DescribeApiVersions().OrderByDescending(o => o.ApiVersion.MajorVersion))
app.MapScalarApiReference($"/openapi/{description.GroupName}", (options, context) =>
{
options.AddServer(new ScalarServer($"https://{context.Request.Host}"));
options.WithOpenApiRoutePattern($"/openapi/{description.GroupName}.json");
options.WithTheme(ScalarTheme.DeepSpace);
options.Agent = new ScalarAgentOptions { Disabled = true };
options.Authentication = new ScalarAuthenticationOptions { PreferredSecuritySchemes = ["Bearer"] };
});
var latestVersionGroup = apiVersions.FirstOrDefault()?.GroupName ?? "v1";
app.MapGet("/", () => Results.Redirect($"/openapi/{latestVersionGroup}"))
.ExcludeFromDescription();
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.Run();