49 lines
1.1 KiB
C#
49 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Watcher.Data;
|
|
using Watcher.Models;
|
|
using Watcher.ViewModels;
|
|
|
|
public class HeartbeatDto
|
|
{
|
|
public string? IpAddress { get; set; }
|
|
}
|
|
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class HeartbeatController : Controller
|
|
{
|
|
|
|
private readonly AppDbContext _context;
|
|
|
|
public HeartbeatController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpPost("receive")]
|
|
public async Task<IActionResult> Receive([FromBody] HeartbeatDto dto)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(dto.IpAddress))
|
|
{
|
|
return BadRequest("Missing IP address.");
|
|
}
|
|
|
|
var server = await _context.Servers
|
|
.FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress);
|
|
|
|
if (server != null)
|
|
{
|
|
server.LastSeen = DateTime.UtcNow;
|
|
await _context.SaveChangesAsync();
|
|
return Ok();
|
|
}
|
|
|
|
return NotFound("No matching server found.");
|
|
}
|
|
}
|