Refactored/recreated server system. Seperated into sub systems. Still wip

This commit is contained in:
2025-05-29 21:56:38 +02:00
parent f2771acb49
commit b955bd3527
32 changed files with 1642 additions and 1174 deletions

View File

@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.Configuration;
using MoonlightServers.Daemon.ServerSystem.SubSystems;
using MoonlightServers.Daemon.Services;
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
@@ -24,12 +24,21 @@ public class DownloadController : Controller
var serverId = int.Parse(User.Claims.First(x => x.Type == "serverId").Value);
var path = User.Claims.First(x => x.Type == "path").Value;
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Read(path,
async dataStream => { await Results.File(dataStream).ExecuteAsync(HttpContext); });
var storageSubSystem = server.GetRequiredSubSystem<StorageSubSystem>();
var fileSystem = await storageSubSystem.GetFileSystem();
await fileSystem.Read(
path,
async dataStream =>
{
await Results.File(dataStream).ExecuteAsync(HttpContext);
}
);
}
}

View File

@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.ServerSystem.SubSystems;
using MoonlightServers.Daemon.Services;
using MoonlightServers.DaemonShared.DaemonSide.Http.Requests;
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
@@ -22,56 +24,41 @@ public class ServerFileSystemController : Controller
[HttpGet("{id:int}/files/list")]
public async Task<ServerFileSystemResponse[]> List([FromRoute] int id, [FromQuery] string path = "")
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
return await server.FileSystem.List(path);
return await fileSystem.List(path);
}
[HttpPost("{id:int}/files/move")]
public async Task Move([FromRoute] int id, [FromQuery] string oldPath, [FromQuery] string newPath)
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Move(oldPath, newPath);
await fileSystem.Move(oldPath, newPath);
}
[HttpDelete("{id:int}/files/delete")]
public async Task Delete([FromRoute] int id, [FromQuery] string path)
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Delete(path);
await fileSystem.Delete(path);
}
[HttpPost("{id:int}/files/mkdir")]
public async Task Mkdir([FromRoute] int id, [FromQuery] string path)
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Mkdir(path);
await fileSystem.Mkdir(path);
}
[HttpPost("{id:int}/files/compress")]
public async Task Compress([FromRoute] int id, [FromBody] ServerFilesCompressRequest request)
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Compress(
await fileSystem.Compress(
request.Items,
request.Destination,
request.Type
@@ -81,15 +68,24 @@ public class ServerFileSystemController : Controller
[HttpPost("{id:int}/files/decompress")]
public async Task Decompress([FromRoute] int id, [FromBody] ServerFilesDecompressRequest request)
{
var server = ServerService.GetServer(id);
var fileSystem = await GetFileSystemById(id);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.FileSystem.Decompress(
await fileSystem.Decompress(
request.Path,
request.Destination,
request.Type
);
}
private async Task<ServerFileSystem> GetFileSystemById(int serverId)
{
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var storageSubSystem = server.GetRequiredSubSystem<StorageSubSystem>();
return await storageSubSystem.GetFileSystem();
}
}

View File

@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.Enums;
using MoonlightServers.Daemon.Services;
using ServerTrigger = MoonlightServers.Daemon.ServerSystem.ServerTrigger;
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
@@ -21,44 +22,44 @@ public class ServerPowerController : Controller
[HttpPost("{serverId:int}/start")]
public async Task Start(int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Start();
await server.Trigger(ServerTrigger.Start);
}
[HttpPost("{serverId:int}/stop")]
public async Task Stop(int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Stop();
await server.Trigger(ServerTrigger.Stop);
}
[HttpPost("{serverId:int}/install")]
public async Task Install(int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Install();
await server.Trigger(ServerTrigger.Install);
}
[HttpPost("{serverId:int}/kill")]
public async Task Kill(int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Kill();
await server.Trigger(ServerTrigger.Kill);
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.ServerSystem.SubSystems;
using MoonlightServers.Daemon.Services;
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
using MoonlightServers.DaemonShared.Enums;
@@ -28,20 +29,20 @@ public class ServersController : Controller
[HttpDelete("{serverId:int}")]
public async Task Delete([FromRoute] int serverId)
{
await ServerService.Delete(serverId);
//await ServerService.Delete(serverId);
}
[HttpGet("{serverId:int}/status")]
public Task<ServerStatusResponse> GetStatus([FromRoute] int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var result = new ServerStatusResponse()
{
State = (ServerState)server.State
State = (ServerState)server.StateMachine.State
};
return Task.FromResult(result);
@@ -50,14 +51,17 @@ public class ServersController : Controller
[HttpGet("{serverId:int}/logs")]
public async Task<ServerLogsResponse> GetLogs([FromRoute] int serverId)
{
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var consoleSubSystem = server.GetRequiredSubSystem<ConsoleSubSystem>();
var messages = await consoleSubSystem.RetrieveCache();
return new ServerLogsResponse()
{
Messages = await server.GetConsoleMessages()
Messages = messages
};
}
}

View File

@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonCore.Helpers;
using MoonlightServers.Daemon.Configuration;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.ServerSystem.SubSystems;
using MoonlightServers.Daemon.Services;
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
@@ -64,14 +64,18 @@ public class UploadController : Controller
#endregion
var server = ServerService.GetServer(serverId);
var server = ServerService.Find(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var storageSubSystem = server.GetRequiredSubSystem<StorageSubSystem>();
var fileSystem = await storageSubSystem.GetFileSystem();
var dataStream = file.OpenReadStream();
await server.FileSystem.CreateChunk(
await fileSystem.CreateChunk(
path,
totalSize,
positionToSkipTo,