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 DatabaseRepository; private readonly NodeService NodeService; private readonly ILogger Logger; public HealthController( DatabaseRepository databaseRepository, NodeService nodeService, ILogger logger ) { DatabaseRepository = databaseRepository; NodeService = nodeService; Logger = logger; } [HttpGet] public async Task> 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 } }; } }