51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using MoonlightServers.Api.Infrastructure.Database;
|
|
using MoonlightServers.Api.Infrastructure.Database.Entities;
|
|
using MoonlightServers.Shared;
|
|
using MoonlightServers.Shared.Admin.Nodes;
|
|
|
|
namespace MoonlightServers.Api.Admin.Nodes;
|
|
|
|
[ApiController]
|
|
[Authorize(Policy = Permissions.Nodes.View)]
|
|
[Route("api/admin/servers/nodes/{id:int}/health")]
|
|
public class HealthController : Controller
|
|
{
|
|
private readonly DatabaseRepository<Node> DatabaseRepository;
|
|
private readonly NodeService NodeService;
|
|
private readonly ILogger<HealthController> Logger;
|
|
|
|
public HealthController(
|
|
DatabaseRepository<Node> databaseRepository,
|
|
NodeService nodeService,
|
|
ILogger<HealthController> logger
|
|
)
|
|
{
|
|
DatabaseRepository = databaseRepository;
|
|
NodeService = nodeService;
|
|
Logger = logger;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<NodeHealthDto>> GetAsync([FromRoute] int id)
|
|
{
|
|
var node = await DatabaseRepository
|
|
.Query()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (node == null)
|
|
return Problem("No node with this id found", statusCode: 404);
|
|
|
|
var health = await NodeService.GetHealthAsync(node);
|
|
|
|
return new NodeHealthDto()
|
|
{
|
|
StatusCode = health.StatusCode,
|
|
RemoteStatusCode = health.Dto?.RemoteStatusCode ?? 0,
|
|
IsHealthy = health is { StatusCode: >= 200 and <= 299, Dto.RemoteStatusCode: >= 200 and <= 299 }
|
|
};
|
|
}
|
|
} |