Re-implemented server state machine. Cleaned up code
TODO: Handle trigger errors
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user