Re-implemented server state machine. Cleaned up code

TODO: Handle trigger errors
This commit is contained in:
2025-02-12 23:02:00 +01:00
parent 4088bfaef5
commit f45699f300
44 changed files with 913 additions and 831 deletions

View File

@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.Enums;
using MoonlightServers.Daemon.Services;
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
[ApiController]
[Route("api/servers")]
public class ServerPowerController : Controller
{
private readonly ServerService ServerService;
public ServerPowerController(ServerService serverService)
{
ServerService = serverService;
}
[HttpPost("{serverId:int}/start")]
public async Task Start(int serverId, [FromQuery] bool runAsync = true)
{
var server = ServerService.GetServer(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Start();
}
[HttpPost("{serverId:int}/stop")]
public async Task Stop(int serverId, [FromQuery] bool runAsync = true)
{
var server = ServerService.GetServer(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.Stop();
}
}

View File

@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonlightServers.Daemon.Models;
using MoonlightServers.Daemon.Services;
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
using MoonlightServers.DaemonShared.Enums;
@@ -28,7 +27,7 @@ public class ServersController : Controller
return new ServerStatusResponse()
{
State = server.State
State = (ServerState)server.State
};
}
@@ -42,18 +41,7 @@ public class ServersController : Controller
return new ServerLogsResponse()
{
Messages = server.Console.Messages
Messages = await server.GetConsoleMessages()
};
}
[HttpPost("{serverId:int}/start")]
public async Task Start(int serverId)
{
var server = ServerService.GetServer(serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
await server.StateMachine.TransitionTo(ServerState.Starting);
}
}

View File

@@ -1,29 +1,26 @@
using Microsoft.AspNetCore.SignalR;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.Models;
using MoonlightServers.Daemon.Services;
using MoonlightServers.DaemonShared.Enums;
namespace MoonlightServers.Daemon.Http.Hubs;
public class ServerConsoleHub : Hub
public class ServerWebSocketHub : Hub
{
private readonly ILogger<ServerConsoleHub> Logger;
private readonly ServerConsoleService ConsoleService;
private readonly ILogger<ServerWebSocketHub> Logger;
private readonly ServerWebSocketService WebSocketService;
public ServerConsoleHub(ILogger<ServerConsoleHub> logger, ServerConsoleService consoleService)
public ServerWebSocketHub(ILogger<ServerWebSocketHub> logger, ServerWebSocketService webSocketService)
{
Logger = logger;
ConsoleService = consoleService;
WebSocketService = webSocketService;
}
#region Connection Handlers
public override async Task OnConnectedAsync()
=> await ConsoleService.InitializeClient(Context);
=> await WebSocketService.InitializeClient(Context);
public override async Task OnDisconnectedAsync(Exception? exception)
=> await ConsoleService.DestroyClient(Context);
=> await WebSocketService.DestroyClient(Context);
#endregion
@@ -34,7 +31,7 @@ public class ServerConsoleHub : Hub
{
try
{
await ConsoleService.AuthenticateClient(Context, accessToken);
await WebSocketService.AuthenticateClient(Context, accessToken);
}
catch (Exception e)
{