Refactored ui. Improved console experience. Added command endpoint
This commit is contained in:
@@ -68,6 +68,7 @@ public class ServersController : Controller
|
||||
.Include(x => x.Star)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.OrderBy(x => x.Id)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedItems = items
|
||||
@@ -213,7 +214,7 @@ public class ServersController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
[Authorize(Policy = "permissions.admin.servers.write")]
|
||||
[Authorize(Policy = "permissions:admin.servers.write")]
|
||||
public async Task<ServerResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
|
||||
{
|
||||
//TODO: Handle shrinking virtual disk
|
||||
@@ -294,11 +295,16 @@ public class ServersController : Controller
|
||||
.Include(x => x.Star)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Backups)
|
||||
.Include(x => x.Allocations)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with that id found", 404);
|
||||
|
||||
server.Variables.Clear();
|
||||
server.Backups.Clear();
|
||||
server.Allocations.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
// If the sync fails on the node and we aren't forcing the deletion,
|
||||
|
||||
@@ -11,6 +11,7 @@ using MoonlightServers.ApiServer.Extensions;
|
||||
using MoonlightServers.ApiServer.Models;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Enums;
|
||||
using MoonlightServers.Shared.Http.Requests.Client.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Client.Servers.Allocations;
|
||||
using MoonlightServers.Shared.Models;
|
||||
@@ -293,6 +294,17 @@ public class ServersController : Controller
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost("{serverId:int}/command")]
|
||||
public async Task RunCommand([FromRoute] int serverId, [FromBody] ServerCommandRequest request)
|
||||
{
|
||||
var server = await GetServerById(
|
||||
serverId,
|
||||
permission => permission is { Name: "console", Type: >= ServerPermissionType.ReadWrite }
|
||||
);
|
||||
|
||||
await ServerService.RunCommand(server, request.Command);
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerById(int serverId, Func<ServerSharePermission, bool>? filter = null)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
|
||||
@@ -6,6 +6,7 @@ using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Requests;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Services;
|
||||
@@ -36,7 +37,7 @@ public class ServerService
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task Stop(Server server)
|
||||
{
|
||||
try
|
||||
@@ -49,7 +50,7 @@ public class ServerService
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task Kill(Server server)
|
||||
{
|
||||
try
|
||||
@@ -64,7 +65,7 @@ public class ServerService
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public async Task Install(Server server)
|
||||
{
|
||||
try
|
||||
@@ -90,7 +91,7 @@ public class ServerService
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task SyncDelete(Server server)
|
||||
{
|
||||
try
|
||||
@@ -103,7 +104,7 @@ public class ServerService
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<ServerStatusResponse> GetStatus(Server server)
|
||||
{
|
||||
try
|
||||
@@ -137,7 +138,27 @@ public class ServerService
|
||||
using var apiClient = await GetApiClient(server);
|
||||
return await apiClient.GetJson<ServerStatsResponse>($"api/servers/{server.Id}/stats");
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RunCommand(Server server, string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var apiClient = await GetApiClient(server);
|
||||
|
||||
await apiClient.Post(
|
||||
$"api/servers/{server.Id}/command",
|
||||
new ServerCommandRequest()
|
||||
{
|
||||
Command = command
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
@@ -149,10 +170,10 @@ public class ServerService
|
||||
{
|
||||
if (server.OwnerId == user.Id)
|
||||
return true;
|
||||
|
||||
|
||||
return PermissionHelper.HasPermission(user.Permissions, "admin.servers.get");
|
||||
}
|
||||
|
||||
|
||||
private async Task<HttpApiClient> GetApiClient(Server server)
|
||||
{
|
||||
var serverWithNode = server;
|
||||
|
||||
Reference in New Issue
Block a user