2022-02-27 16:40:51 +00:00
|
|
|
import http from '@/api/http';
|
2022-02-27 17:25:31 +00:00
|
|
|
import { Server, ServerEggVariable, Transformers } from '@definitions/user';
|
2022-02-27 16:47:22 +00:00
|
|
|
|
|
|
|
interface TokenResponse {
|
|
|
|
token: string;
|
|
|
|
socket: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ServerPowerState = 'offline' | 'starting' | 'running' | 'stopping';
|
|
|
|
|
|
|
|
export interface ServerStats {
|
|
|
|
status: ServerPowerState;
|
|
|
|
isSuspended: boolean;
|
|
|
|
memoryUsageInBytes: number;
|
|
|
|
cpuUsagePercent: number;
|
|
|
|
diskUsageInBytes: number;
|
|
|
|
networkRxInBytes: number;
|
|
|
|
networkTxInBytes: number;
|
|
|
|
uptime: number;
|
|
|
|
}
|
2022-02-27 16:40:51 +00:00
|
|
|
|
|
|
|
const getServer = async (uuid: string): Promise<[ Server, string[] ]> => {
|
|
|
|
const { data } = await http.get(`/api/client/servers/${uuid}`);
|
|
|
|
|
|
|
|
return [
|
|
|
|
Transformers.toServer(data),
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
data.meta?.is_server_owner ? [ '*' ] : (data.meta?.user_permissions || []),
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-02-27 16:47:22 +00:00
|
|
|
const getWebsocketToken = async (server: string): Promise<TokenResponse> => {
|
|
|
|
const { data } = await http.get(`/api/client/servers/${server}/websocket`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
token: data.data.token,
|
|
|
|
socket: data.data.socket,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const renameServer = async (uuid: string, name: string): Promise<void> => {
|
|
|
|
await http.post(`/api/client/servers/${uuid}/settings/rename`, { name });
|
|
|
|
};
|
|
|
|
|
|
|
|
const reinstallServer = async (uuid: string): Promise<void> => {
|
|
|
|
await http.post(`/api/client/servers/${uuid}/settings/reinstall`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setSelectedDockerImage = async (uuid: string, image: string): Promise<void> => {
|
|
|
|
await http.put(`/api/client/servers/${uuid}/settings/docker-image`, { docker_image: image });
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateStartupVariable = async (uuid: string, key: string, value: string): Promise<[ ServerEggVariable, string ]> => {
|
|
|
|
const { data } = await http.put(`/api/client/servers/${uuid}/startup/variable`, { key, value });
|
|
|
|
|
2022-02-27 17:25:31 +00:00
|
|
|
return [ Transformers.toServerEggVariable(data), data.meta.startup_command ];
|
2022-02-27 16:47:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getServerResourceUsage = async (server: string): Promise<ServerStats> => {
|
|
|
|
const { data } = await http.get(`/api/client/servers/${server}/resources`);
|
|
|
|
const { attributes } = data;
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: attributes.current_state,
|
|
|
|
isSuspended: attributes.is_suspended,
|
|
|
|
memoryUsageInBytes: attributes.resources.memory_bytes,
|
|
|
|
cpuUsagePercent: attributes.resources.cpu_absolute,
|
|
|
|
diskUsageInBytes: attributes.resources.disk_bytes,
|
|
|
|
networkRxInBytes: attributes.resources.network_rx_bytes,
|
|
|
|
networkTxInBytes: attributes.resources.network_tx_bytes,
|
|
|
|
uptime: attributes.resources.uptime,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
getServer,
|
|
|
|
getWebsocketToken,
|
|
|
|
renameServer,
|
|
|
|
reinstallServer,
|
|
|
|
setSelectedDockerImage,
|
|
|
|
updateStartupVariable,
|
|
|
|
getServerResourceUsage,
|
|
|
|
};
|