Project
This commit is contained in:
60
HealtRegistry/Program.cs
Normal file
60
HealtRegistry/Program.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user