From 90dacceb62618938bbedac526e4e7284e0fc3de9 Mon Sep 17 00:00:00 2001 From: daniel-hbn Date: Mon, 16 Jun 2025 20:12:13 +0200 Subject: [PATCH] =?UTF-8?q?Endpoint-Ger=C3=BCst=20erstellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Watcher/Controllers/MonitoringController.cs | 123 ++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 Watcher/Controllers/MonitoringController.cs diff --git a/Watcher/Controllers/MonitoringController.cs b/Watcher/Controllers/MonitoringController.cs new file mode 100644 index 0000000..f6c3fa1 --- /dev/null +++ b/Watcher/Controllers/MonitoringController.cs @@ -0,0 +1,123 @@ +using System.ComponentModel.DataAnnotations; +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; + +namespace Watcher.Controllers; + +public class RegistrationDto +{ + // Server Identity + [Required] + public int Id { get; set; } + + [Required] + public string? IpAddress { get; set; } + + // Hardware Info + [Required] + public string? CpuType { get; set; } + + [Required] + public int CpuCores { get; set; } + + [Required] + public string? GpuType { get; set; } + +} + +public class MetricDto +{ + // Server Identity + [Required] + public int Id { get; set; } + + [Required] + public string? IpAddress { get; set; } + + // Metrics + // CPU Auslastung, CPU Temperatur + // GPU Auslastung, GPU Temperatur + // RAM Auslastung + // Disks? +} + +[ApiController] +[Route("[monitoring]")] +public class MonitoringController : Controller +{ + private readonly AppDbContext _context; + + public MonitoringController(AppDbContext context) + { + _context = context; + } + + [HttpPost("register")] + public async Task Register([FromBody] RegistrationDto dto) + { + // Gültigkeit des Payloads prüfen + if (!ModelState.IsValid) + { + var errors = ModelState.Values + .SelectMany(v => v.Errors) + .Select(e => e.ErrorMessage) + .ToList(); + + return BadRequest(new { error = "Ungültiger Payload", details = errors }); + } + + // Später Key-Checks hier + + // Server in Datenbank finden + var server = await _context.Servers + .FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress); + + if (server != null) + { + // Serverdaten in Datenbank eintragen + + // Success + return Ok(); + } + + return NotFound("No Matching Server found."); + + } + + [HttpPost("metric")] + public async Task Receive([FromBody] MetricDto dto) + { + // Gültigkeit des Payloads prüfen + if (!ModelState.IsValid) + { + var errors = ModelState.Values + .SelectMany(v => v.Errors) + .Select(e => e.ErrorMessage) + .ToList(); + + return BadRequest(new { error = "Ungültiger Payload", details = errors }); + } + + // Server in Datenbank finden + var server = await _context.Servers + .FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress); + + if (server != null) + { + // Serverdaten in Datenbank eintragen + + // Success + return Ok(); + } + + return NotFound("No Matching Server found."); + + } +} \ No newline at end of file