Consolidate many of the server api files into one

This commit is contained in:
Dane Everitt 2022-02-27 11:47:22 -05:00
parent 5b5e3f26f4
commit e908c391ee
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
15 changed files with 81 additions and 90 deletions

View file

@ -1,31 +0,0 @@
import http from '@/api/http';
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;
}
export default (server: string): Promise<ServerStats> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${server}/resources`)
.then(({ data: { attributes } }) => resolve({
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,
}))
.catch(reject);
});
};

View file

@ -1,17 +0,0 @@
import http from '@/api/http';
interface Response {
token: string;
socket: string;
}
export default (server: string): Promise<Response> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${server}/websocket`)
.then(({ data }) => resolve({
token: data.data.token,
socket: data.data.socket,
}))
.catch(reject);
});
};

View file

@ -1,5 +1,25 @@
import http from '@/api/http';
import { Transformers, Server } from '@definitions/user';
import { Server, Transformers } from '@definitions/user';
import { ServerEggVariable } from '@/api/server/types';
import { rawDataToServerEggVariable } from '@/api/transformers';
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;
}
const getServer = async (uuid: string): Promise<[ Server, string[] ]> => {
const { data } = await http.get(`/api/client/servers/${uuid}`);
@ -11,4 +31,55 @@ const getServer = async (uuid: string): Promise<[ Server, string[] ]> => {
];
};
export { getServer };
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 });
return [ rawDataToServerEggVariable(data), data.meta.startup_command ];
};
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,
};

View file

@ -1,9 +0,0 @@
import http from '@/api/http';
export default (uuid: string): Promise<void> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/settings/reinstall`)
.then(() => resolve())
.catch(reject);
});
};

View file

@ -1,9 +0,0 @@
import http from '@/api/http';
export default (uuid: string, name: string): Promise<void> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/settings/rename`, { name })
.then(() => resolve())
.catch(reject);
});
};

View file

@ -1,5 +0,0 @@
import http from '@/api/http';
export default async (uuid: string, image: string): Promise<void> => {
await http.put(`/api/client/servers/${uuid}/settings/docker-image`, { docker_image: image });
};

View file

@ -1,9 +0,0 @@
import http from '@/api/http';
import { ServerEggVariable } from '@/api/server/types';
import { rawDataToServerEggVariable } from '@/api/transformers';
export default async (uuid: string, key: string, value: string): Promise<[ ServerEggVariable, string ]> => {
const { data } = await http.put(`/api/client/servers/${uuid}/startup/variable`, { key, value });
return [ rawDataToServerEggVariable(data), data.meta.startup_command ];
};

View file

@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faEthernet, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
import { Link } from 'react-router-dom';
import { Server } from '@definitions/user';
import getServerResourceUsage, { ServerPowerState, ServerStats } from '@/api/server/getServerResourceUsage';
import { getServerResourceUsage, ServerPowerState, ServerStats } from '@/api/server';
import { bytesToHuman, megabytesToHuman, formatIp } from '@/helpers';
import tw, { styled } from 'twin.macro';
import GreyRowBox from '@/components/elements/GreyRowBox';

View file

@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { Websocket } from '@/plugins/Websocket';
import { ServerContext } from '@/state/server';
import getWebsocketToken from '@/api/server/getWebsocketToken';
import { getWebsocketToken } from '@/api/server';
import ContentContainer from '@/components/elements/ContentContainer';
import { CSSTransition } from 'react-transition-group';
import Spinner from '@/components/elements/Spinner';

View file

@ -7,7 +7,7 @@ import FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { SocketEvent, SocketRequest } from '@/components/server/events';
import Field from '@/components/elements/Field';
import updateStartupVariable from '@/api/server/updateStartupVariable';
import { updateStartupVariable } from '@/api/server';
import { Form, Formik } from 'formik';
interface Values {

View file

@ -3,7 +3,7 @@ import { ServerContext } from '@/state/server';
import Modal from '@/components/elements/Modal';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import setSelectedDockerImage from '@/api/server/setSelectedDockerImage';
import { setSelectedDockerImage } from '@/api/server';
import FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { SocketEvent, SocketRequest } from '@/components/server/events';

View file

@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import ConfirmationModal from '@/components/elements/ConfirmationModal';
import reinstallServer from '@/api/server/reinstallServer';
import { reinstallServer } from '@/api/server';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http';

View file

@ -3,7 +3,7 @@ import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Actions, useStoreActions } from 'easy-peasy';
import renameServer from '@/api/server/renameServer';
import { renameServer } from '@/api/server';
import Field from '@/components/elements/Field';
import { object, string } from 'yup';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';

View file

@ -12,7 +12,7 @@ import { useDeepCompareEffect } from '@/plugins/useDeepCompareEffect';
import Select from '@/components/elements/Select';
import isEqual from 'react-fast-compare';
import Input from '@/components/elements/Input';
import setSelectedDockerImage from '@/api/server/setSelectedDockerImage';
import { setSelectedDockerImage } from '@/api/server';
import InputSpinner from '@/components/elements/InputSpinner';
import useFlash from '@/plugins/useFlash';

View file

@ -7,7 +7,7 @@ import Input from '@/components/elements/Input';
import Switch from '@/components/elements/Switch';
import tw from 'twin.macro';
import { debounce } from 'debounce';
import updateStartupVariable from '@/api/server/updateStartupVariable';
import { updateStartupVariable } from '@/api/server';
import useFlash from '@/plugins/useFlash';
import FlashMessageRender from '@/components/FlashMessageRender';
import getServerStartup from '@/api/swr/getServerStartup';