Files
healthr/HealtRegistry/Program.cs
2024-01-23 17:50:29 +01:00

61 lines
1.6 KiB
C#

using HealtRegistry.Models;
using HealtRegistry.Services;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IHealthService, HealthService>();
builder.Services.AddHostedService<HealthTimeoutService>();
builder.Services.AddOptions<ServicesConfiguration>()
.Bind(builder.Configuration)
.ValidateDataAnnotations();
if (builder.Environment.IsProduction())
{
builder.Configuration.AddJsonFile("/config/appsettings.json", optional: true, reloadOnChange: true);
}
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapPost("/health/{service}", ([FromRoute] string service, IHealthService healthService) =>
{
healthService.SetServiceHealth(service, true);
return Results.Ok();
})
.WithOpenApi();
app.MapPut("/health/{service}", ([FromRoute] string service, [FromBody] HealthRequest requestBody, IHealthService healthService, [FromServices] ILoggerFactory loggerFactory) =>
{
if (requestBody.Health.HasValue)
{
healthService.SetServiceHealth(service, requestBody.Health.Value);
}
else
{
var logger = loggerFactory.CreateLogger("Health");
logger.LogDebug("Health request body does not contains 'Health' property");
}
return Results.Ok();
})
.WithOpenApi();
app.MapGet("/status", (IHealthService healthService) =>
{
return healthService.GetServiceHealthes();
})
.WithOpenApi();
app.Run();