Implemented server list and power state display
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using MoonlightServers.DaemonShared.Enums;
|
||||
using MoonlightServers.Shared.Enums;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Extensions;
|
||||
|
||||
public static class ServerStateExtensions
|
||||
{
|
||||
public static ServerPowerState ToServerPowerState(this ServerState state)
|
||||
{
|
||||
return state switch
|
||||
{
|
||||
ServerState.Installing => ServerPowerState.Installing,
|
||||
ServerState.Stopping => ServerPowerState.Stopping,
|
||||
ServerState.Online => ServerPowerState.Online,
|
||||
ServerState.Starting => ServerPowerState.Starting,
|
||||
ServerState.Offline => ServerPowerState.Offline,
|
||||
_ => ServerPowerState.Offline
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,7 @@ using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Servers;
|
||||
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extensions;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Extensions;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Http.Responses.User.Allocations;
|
||||
using MoonlightServers.Shared.Http.Responses.Users.Servers;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/servers")]
|
||||
public class ServersController : Controller
|
||||
{
|
||||
private readonly DatabaseRepository<Server> ServerRepository;
|
||||
private readonly NodeService NodeService;
|
||||
|
||||
public ServersController(DatabaseRepository<Server> serverRepository, NodeService nodeService)
|
||||
{
|
||||
ServerRepository = serverRepository;
|
||||
NodeService = nodeService;
|
||||
}
|
||||
|
||||
[HttpGet("list")]
|
||||
[RequirePermission("meta.authenticated")]
|
||||
public async Task<PagedData<ServerDetailResponse>> List([FromQuery] int page, [FromQuery] int pageSize)
|
||||
{
|
||||
var user = User.AsIdentity<User>();
|
||||
|
||||
var query = ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Star)
|
||||
.Include(x => x.Node)
|
||||
.Where(x => x.OwnerId == user.Id);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
var items = await query.Skip(page * pageSize).Take(pageSize).ToArrayAsync();
|
||||
|
||||
var mappedItems = items.Select(x => new ServerDetailResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
NodeName = x.Node.Name,
|
||||
StarName = x.Star.Name,
|
||||
Allocations = x.Allocations.Select(y => new AllocationDetailResponse()
|
||||
{
|
||||
Id = y.Id,
|
||||
Port = y.Port,
|
||||
IpAddress = y.IpAddress
|
||||
}).ToArray()
|
||||
}).ToArray();
|
||||
|
||||
return new PagedData<ServerDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{serverId:int}/status")]
|
||||
[RequirePermission("meta.authenticated")]
|
||||
public async Task<ServerStatusResponse> GetStatus([FromRoute] int serverId)
|
||||
{
|
||||
var server = await GetServerWithPermCheck(serverId);
|
||||
|
||||
var apiClient = await NodeService.CreateApiClient(server.Node);
|
||||
|
||||
try
|
||||
{
|
||||
var data = await apiClient.GetJson<DaemonShared.DaemonSide.Http.Responses.Servers.ServerStatusResponse>(
|
||||
$"api/servers/{server.Id}/status"
|
||||
);
|
||||
|
||||
return new ServerStatusResponse()
|
||||
{
|
||||
PowerState = data.State.ToServerPowerState()
|
||||
};
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
throw new HttpApiException("Unable to access the node the server is running on", 502);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Server> GetServerWithPermCheck(int serverId)
|
||||
{
|
||||
var user = User.AsIdentity<User>();
|
||||
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
if (server.OwnerId == user.Id) // The current user is the owner
|
||||
return server;
|
||||
|
||||
if (User.HasPermission("admin.servers.get")) // The current user is an admin
|
||||
return server;
|
||||
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user