Endpoint-Gerüst erstellt
This commit is contained in:
123
Watcher/Controllers/MonitoringController.cs
Normal file
123
Watcher/Controllers/MonitoringController.cs
Normal file
@@ -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<IActionResult> 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<IActionResult> 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.");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user