Recreated project with project template

This commit is contained in:
2024-12-05 15:35:23 +01:00
parent 7659403dc8
commit 3392407890
76 changed files with 2020 additions and 2922 deletions

View File

@@ -1,63 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using Moonlight.ApiServer.App.Attributes;
using Moonlight.ApiServer.App.Exceptions;
using Moonlight.ApiServer.App.Helpers;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.Shared.Http.Requests.Admin.Allocations;
using MoonlightServers.Shared.Http.Responses.Admin.Allocations;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
[ApiController]
[Route("admin/servers/nodes/{rootItem:int}/allocations")]
public class NodeAllocationsController : BaseSubCrudController<Node, Allocation, DetailAllocationResponse, CreateAllocationRequest, DetailAllocationResponse, UpdateAllocationRequest, DetailAllocationResponse>
{
public override Func<Node, List<Allocation>> Property => node => node.Allocations;
public NodeAllocationsController(DatabaseRepository<Allocation> itemRepository, DatabaseRepository<Node> rootItemRepository, IHttpContextAccessor contextAccessor) : base(itemRepository, rootItemRepository, contextAccessor)
{
PermissionPrefix = "admin.servers.nodes.allocations";
}
[HttpPost]
[RequirePermission("admin.servers.nodes.allocations.create")]
public override async Task<ActionResult<DetailAllocationResponse>> Create(CreateAllocationRequest request)
{
if (ItemRepository.Get().Any(x => x.IpAddress == request.IpAddress && x.Port == request.Port))
throw new ApiException("An allocation with this ip and port already exists", statusCode: 400);
var item = Mapper.Map<Allocation>(request!);
Property.Invoke(RootItem).Add(item);
RootItemRepository.Update(RootItem);
var response = Mapper.Map<DetailAllocationResponse>(item);
return Ok(response);
}
[HttpPatch("{id}")]
[RequirePermission("admin.servers.nodes.allocations.create")]
public override async Task<ActionResult<DetailAllocationResponse>> Update(int id, UpdateAllocationRequest request)
{
var item = LoadItemById(id);
if (ItemRepository.Get().Any(x => x.IpAddress == request.IpAddress && x.Port == request.Port && x.Id != item.Id))
throw new ApiException("An allocation with this ip and port already exists", statusCode: 400);
var mappedItem = Mapper.Map(item, request!, ignoreNullValues: true);
ItemRepository.Update(mappedItem);
var response = Mapper.Map<DetailAllocationResponse>(mappedItem);
return Ok(response);
}
protected override IEnumerable<Node> IncludeRelations(IQueryable<Node> items)
=> items.Include(x => x.Allocations);
}

View File

@@ -1,105 +0,0 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using Moonlight.ApiServer.App.Attributes;
using Moonlight.ApiServer.App.Exceptions;
using Moonlight.ApiServer.App.Helpers;
using MoonlightServers.DaemonShared.Http.Resources.Sys;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Extensions;
using MoonlightServers.Shared.Http.Requests.Admin.Nodes;
using MoonlightServers.Shared.Http.Responses.Admin.Nodes;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
[ApiController]
[Route("admin/servers/nodes")]
public class NodesController : BaseCrudController<Node, DetailNodeResponse, CreateNodeRequest, DetailNodeResponse, UpdateNodeRequest, DetailNodeResponse>
{
public NodesController(DatabaseRepository<Node> itemRepository) : base(itemRepository)
{
PermissionPrefix = "admin.servers.nodes";
}
[HttpPost]
[RequirePermission("admin.servers.nodes.create")]
public override async Task<ActionResult<DetailNodeResponse>> Create(CreateNodeRequest request)
{
ValidateFqdn(request.Fqdn, request.SslEnabled);
var node = Mapper.Map<Node>(request);
node.Token = Formatter.GenerateString(32);
var finalNode = ItemRepository.Add(node);
return Ok(Mapper.Map<DetailNodeResponse>(finalNode));
}
[HttpPatch("{id}")]
[RequirePermission("admin.servers.nodes.update")]
public override async Task<ActionResult<DetailNodeResponse>> Update(int id, UpdateNodeRequest request)
{
ValidateFqdn(request.Fqdn, request.SslEnabled);
var item = LoadItemById(id);
item = Mapper.Map(item, request);
ItemRepository.Update(item);
return Ok(Mapper.Map<DetailNodeResponse>(item));
}
[HttpGet("{id}/status")]
[RequirePermission("admin.servers.nodes.status")]
public async Task<ActionResult<StatusNodeResponse>> Status(int id)
{
var node = LoadItemById(id);
using var httpClient = node.CreateClient();
SystemInfoResponse response;
try
{
response = await httpClient.GetJson<SystemInfoResponse>("system/info");
}
catch (HttpRequestException e)
{
throw new ApiException(
"The requested node's api server was not reachable",
e.Message,
statusCode: 502
);
}
var result = Mapper.Map<StatusNodeResponse>(response);
return Ok(result);
}
private void ValidateFqdn(string fqdn, bool ssl)
{
if (ssl)
{
// Is it a valid domain?
if (Regex.IsMatch(fqdn, "^(?!-)(?:[a-zA-Z\\d-]{0,62}[a-zA-Z\\d]\\.)+(?:[a-zA-Z]{2,})$"))
return;
throw new ApiException("The fqdn needs to be a valid domain. If you want to use an ip address as the fqdn, disable ssl for this node", statusCode: 400);
}
else
{
// Is it a valid domain?
if (Regex.IsMatch(fqdn, "^(?!-)(?:[a-zA-Z\\d-]{0,62}[a-zA-Z\\d]\\.)+(?:[a-zA-Z]{2,})$"))
return;
// Is it a valid ip?
if (Regex.IsMatch(fqdn, "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
return;
throw new ApiException("The fqdn needs to be either a domain or an ip", statusCode: 400);
}
}
}

View File

@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Mvc;
using MoonlightServers.ApiServer.Services;
using MoonlightServers.Shared.Http.Responses;
namespace MoonlightServers.ApiServer.Http.Controllers;
[ApiController]
[Route("api/example")]
public class ExampleController : Controller
{
private readonly ExampleService ExampleService;
public ExampleController(ExampleService exampleService)
{
ExampleService = exampleService;
}
[HttpGet]
public async Task<ExampleResponse> Get()
{
var val = await ExampleService.GetValue();
return new ExampleResponse()
{
Number = val
};
}
}