Started implementing server service and daemon controllers
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonlightServers.Daemon.ServerSystem.Enums;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/servers/{id:int}")]
|
||||
public class PowerController : Controller
|
||||
{
|
||||
private readonly ServerService ServerService;
|
||||
|
||||
public PowerController(ServerService serverService)
|
||||
{
|
||||
ServerService = serverService;
|
||||
}
|
||||
|
||||
[HttpPost("start")]
|
||||
public async Task<ActionResult> Start([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
if (!server.StateMachine.CanFire(ServerTrigger.Start))
|
||||
return Problem("Cannot fire start trigger in this state");
|
||||
|
||||
await server.StateMachine.FireAsync(ServerTrigger.Start);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("stop")]
|
||||
public async Task<ActionResult> Stop([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
if (!server.StateMachine.CanFire(ServerTrigger.Stop))
|
||||
return Problem("Cannot fire stop trigger in this state");
|
||||
|
||||
await server.StateMachine.FireAsync(ServerTrigger.Stop);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("kill")]
|
||||
public async Task<ActionResult> Kill([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
if (!server.StateMachine.CanFire(ServerTrigger.Kill))
|
||||
return Problem("Cannot fire kill trigger in this state");
|
||||
|
||||
await server.StateMachine.FireAsync(ServerTrigger.Kill);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("install")]
|
||||
public async Task<ActionResult> Install([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
if (!server.StateMachine.CanFire(ServerTrigger.Install))
|
||||
return Problem("Cannot fire install trigger in this state");
|
||||
|
||||
await server.StateMachine.FireAsync(ServerTrigger.Install);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonlightServers.Daemon.Mappers;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
|
||||
using MoonlightServers.DaemonShared.Enums;
|
||||
using MoonlightServers.DaemonShared.PanelSide.Http.Responses;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/servers/{id:int}")]
|
||||
public class ServersController : Controller
|
||||
{
|
||||
private readonly ServerService ServerService;
|
||||
private readonly ServerConfigurationMapper ConfigurationMapper;
|
||||
|
||||
public ServersController(ServerService serverService, ServerConfigurationMapper configurationMapper)
|
||||
{
|
||||
ServerService = serverService;
|
||||
ConfigurationMapper = configurationMapper;
|
||||
}
|
||||
|
||||
[HttpPost("sync")]
|
||||
public async Task<ActionResult> Sync([FromRoute] int id)
|
||||
{
|
||||
await ServerService.InitializeById(id);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("status")]
|
||||
public async Task<ActionResult<ServerStatusResponse>> Status([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
return new ServerStatusResponse()
|
||||
{
|
||||
State = (ServerState)server.StateMachine.State
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("logs")]
|
||||
public async Task<ActionResult<ServerLogsResponse>> Logs([FromRoute] int id)
|
||||
{
|
||||
var server = ServerService.GetById(id);
|
||||
|
||||
if (server == null)
|
||||
return Problem("No server with this id found", statusCode: 404);
|
||||
|
||||
var messages = await server.Console.GetCacheAsync();
|
||||
|
||||
return new ServerLogsResponse()
|
||||
{
|
||||
Messages = messages.ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("stats")]
|
||||
public async Task<ServerStatsResponse> GetStats([FromRoute] int id)
|
||||
{
|
||||
return new ServerStatsResponse()
|
||||
{
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user