From e908c391ee3e853188cb027a751417964e46d63e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 27 Feb 2022 11:47:22 -0500 Subject: [PATCH] Consolidate many of the server api files into one --- .../api/server/getServerResourceUsage.ts | 31 -------- .../scripts/api/server/getWebsocketToken.ts | 17 ----- resources/scripts/api/server/index.ts | 75 ++++++++++++++++++- .../scripts/api/server/reinstallServer.ts | 9 --- resources/scripts/api/server/renameServer.ts | 9 --- .../api/server/setSelectedDockerImage.ts | 5 -- .../api/server/updateStartupVariable.ts | 9 --- .../components/dashboard/ServerRow.tsx | 2 +- .../components/server/WebsocketHandler.tsx | 2 +- .../server/features/GSLTokenModalFeature.tsx | 2 +- .../features/JavaVersionModalFeature.tsx | 2 +- .../server/settings/ReinstallServerBox.tsx | 2 +- .../server/settings/RenameServerBox.tsx | 2 +- .../server/startup/StartupContainer.tsx | 2 +- .../components/server/startup/VariableBox.tsx | 2 +- 15 files changed, 81 insertions(+), 90 deletions(-) delete mode 100644 resources/scripts/api/server/getServerResourceUsage.ts delete mode 100644 resources/scripts/api/server/getWebsocketToken.ts delete mode 100644 resources/scripts/api/server/reinstallServer.ts delete mode 100644 resources/scripts/api/server/renameServer.ts delete mode 100644 resources/scripts/api/server/setSelectedDockerImage.ts delete mode 100644 resources/scripts/api/server/updateStartupVariable.ts diff --git a/resources/scripts/api/server/getServerResourceUsage.ts b/resources/scripts/api/server/getServerResourceUsage.ts deleted file mode 100644 index 2a4c01cf6..000000000 --- a/resources/scripts/api/server/getServerResourceUsage.ts +++ /dev/null @@ -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 => { - 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); - }); -}; diff --git a/resources/scripts/api/server/getWebsocketToken.ts b/resources/scripts/api/server/getWebsocketToken.ts deleted file mode 100644 index da78dfd05..000000000 --- a/resources/scripts/api/server/getWebsocketToken.ts +++ /dev/null @@ -1,17 +0,0 @@ -import http from '@/api/http'; - -interface Response { - token: string; - socket: string; -} - -export default (server: string): Promise => { - 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); - }); -}; diff --git a/resources/scripts/api/server/index.ts b/resources/scripts/api/server/index.ts index e8bc5f116..ba57ca90e 100644 --- a/resources/scripts/api/server/index.ts +++ b/resources/scripts/api/server/index.ts @@ -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 => { + 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 => { + await http.post(`/api/client/servers/${uuid}/settings/rename`, { name }); +}; + +const reinstallServer = async (uuid: string): Promise => { + await http.post(`/api/client/servers/${uuid}/settings/reinstall`); +}; + +const setSelectedDockerImage = async (uuid: string, image: string): Promise => { + 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 => { + 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, +}; diff --git a/resources/scripts/api/server/reinstallServer.ts b/resources/scripts/api/server/reinstallServer.ts deleted file mode 100644 index 5cb2ca5e7..000000000 --- a/resources/scripts/api/server/reinstallServer.ts +++ /dev/null @@ -1,9 +0,0 @@ -import http from '@/api/http'; - -export default (uuid: string): Promise => { - return new Promise((resolve, reject) => { - http.post(`/api/client/servers/${uuid}/settings/reinstall`) - .then(() => resolve()) - .catch(reject); - }); -}; diff --git a/resources/scripts/api/server/renameServer.ts b/resources/scripts/api/server/renameServer.ts deleted file mode 100644 index a4a95141e..000000000 --- a/resources/scripts/api/server/renameServer.ts +++ /dev/null @@ -1,9 +0,0 @@ -import http from '@/api/http'; - -export default (uuid: string, name: string): Promise => { - return new Promise((resolve, reject) => { - http.post(`/api/client/servers/${uuid}/settings/rename`, { name }) - .then(() => resolve()) - .catch(reject); - }); -}; diff --git a/resources/scripts/api/server/setSelectedDockerImage.ts b/resources/scripts/api/server/setSelectedDockerImage.ts deleted file mode 100644 index 70042f3a6..000000000 --- a/resources/scripts/api/server/setSelectedDockerImage.ts +++ /dev/null @@ -1,5 +0,0 @@ -import http from '@/api/http'; - -export default async (uuid: string, image: string): Promise => { - await http.put(`/api/client/servers/${uuid}/settings/docker-image`, { docker_image: image }); -}; diff --git a/resources/scripts/api/server/updateStartupVariable.ts b/resources/scripts/api/server/updateStartupVariable.ts deleted file mode 100644 index 74498caa8..000000000 --- a/resources/scripts/api/server/updateStartupVariable.ts +++ /dev/null @@ -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 ]; -}; diff --git a/resources/scripts/components/dashboard/ServerRow.tsx b/resources/scripts/components/dashboard/ServerRow.tsx index 68c0b9d4f..0972a43c2 100644 --- a/resources/scripts/components/dashboard/ServerRow.tsx +++ b/resources/scripts/components/dashboard/ServerRow.tsx @@ -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'; diff --git a/resources/scripts/components/server/WebsocketHandler.tsx b/resources/scripts/components/server/WebsocketHandler.tsx index 724937f01..1c45b4e5a 100644 --- a/resources/scripts/components/server/WebsocketHandler.tsx +++ b/resources/scripts/components/server/WebsocketHandler.tsx @@ -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'; diff --git a/resources/scripts/components/server/features/GSLTokenModalFeature.tsx b/resources/scripts/components/server/features/GSLTokenModalFeature.tsx index 6cd96ec0d..f6ade73ca 100644 --- a/resources/scripts/components/server/features/GSLTokenModalFeature.tsx +++ b/resources/scripts/components/server/features/GSLTokenModalFeature.tsx @@ -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 { diff --git a/resources/scripts/components/server/features/JavaVersionModalFeature.tsx b/resources/scripts/components/server/features/JavaVersionModalFeature.tsx index f15b9abbb..9ad27f53a 100644 --- a/resources/scripts/components/server/features/JavaVersionModalFeature.tsx +++ b/resources/scripts/components/server/features/JavaVersionModalFeature.tsx @@ -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'; diff --git a/resources/scripts/components/server/settings/ReinstallServerBox.tsx b/resources/scripts/components/server/settings/ReinstallServerBox.tsx index 2ef8af921..a915432c0 100644 --- a/resources/scripts/components/server/settings/ReinstallServerBox.tsx +++ b/resources/scripts/components/server/settings/ReinstallServerBox.tsx @@ -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'; diff --git a/resources/scripts/components/server/settings/RenameServerBox.tsx b/resources/scripts/components/server/settings/RenameServerBox.tsx index 8ed91ca31..f2599ee8e 100644 --- a/resources/scripts/components/server/settings/RenameServerBox.tsx +++ b/resources/scripts/components/server/settings/RenameServerBox.tsx @@ -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'; diff --git a/resources/scripts/components/server/startup/StartupContainer.tsx b/resources/scripts/components/server/startup/StartupContainer.tsx index b5bb1fea5..a75bfd9e9 100644 --- a/resources/scripts/components/server/startup/StartupContainer.tsx +++ b/resources/scripts/components/server/startup/StartupContainer.tsx @@ -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'; diff --git a/resources/scripts/components/server/startup/VariableBox.tsx b/resources/scripts/components/server/startup/VariableBox.tsx index 3ab70a285..508927901 100644 --- a/resources/scripts/components/server/startup/VariableBox.tsx +++ b/resources/scripts/components/server/startup/VariableBox.tsx @@ -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';