2021-10-09 19:02:32 +00:00
|
|
|
import useSWR, { SWRResponse } from 'swr';
|
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
|
|
|
import http from '@/api/http';
|
|
|
|
import { Model, UUID, withRelationships, WithRelationships } from '@/api/admin/index';
|
2021-10-10 18:32:07 +00:00
|
|
|
import { Allocation, Node } from '@/api/admin/node';
|
2022-02-27 16:40:51 +00:00
|
|
|
import { Transformers, User } from '@definitions/admin';
|
2021-10-10 18:32:07 +00:00
|
|
|
import { Egg, EggVariable } from '@/api/admin/egg';
|
2021-10-10 20:13:10 +00:00
|
|
|
import { Nest } from '@/api/admin/nest';
|
2021-10-09 19:02:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the limits for a server that exists on the Panel.
|
|
|
|
*/
|
|
|
|
interface ServerLimits {
|
|
|
|
memory: number;
|
|
|
|
swap: number;
|
|
|
|
disk: number;
|
|
|
|
io: number;
|
|
|
|
cpu: number;
|
|
|
|
threads: string | null;
|
|
|
|
oomDisabled: boolean;
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:32:07 +00:00
|
|
|
export interface ServerVariable extends EggVariable {
|
|
|
|
serverValue: string;
|
|
|
|
}
|
|
|
|
|
2021-10-09 19:02:32 +00:00
|
|
|
/**
|
|
|
|
* Defines a single server instance that is returned from the Panel's admin
|
|
|
|
* API endpoints.
|
|
|
|
*/
|
|
|
|
export interface Server extends Model {
|
|
|
|
id: number;
|
|
|
|
uuid: UUID;
|
|
|
|
externalId: string | null;
|
|
|
|
identifier: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
status: string;
|
|
|
|
userId: number;
|
|
|
|
nodeId: number;
|
|
|
|
allocationId: number;
|
|
|
|
eggId: number;
|
2021-10-10 20:13:10 +00:00
|
|
|
nestId: number;
|
2021-10-09 19:02:32 +00:00
|
|
|
limits: ServerLimits;
|
|
|
|
featureLimits: {
|
|
|
|
databases: number;
|
|
|
|
allocations: number;
|
|
|
|
backups: number;
|
|
|
|
};
|
|
|
|
container: {
|
2021-11-01 18:23:06 +00:00
|
|
|
startup: string | null;
|
2021-10-09 19:02:32 +00:00
|
|
|
image: string;
|
|
|
|
environment: Record<string, string>;
|
|
|
|
};
|
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
|
|
|
relationships: {
|
|
|
|
allocations?: Allocation[];
|
2021-10-10 20:13:10 +00:00
|
|
|
nest?: Nest;
|
2021-10-09 19:02:32 +00:00
|
|
|
egg?: Egg;
|
|
|
|
node?: Node;
|
|
|
|
user?: User;
|
|
|
|
variables?: ServerVariable[];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A standard API response with the minimum viable details for the frontend
|
|
|
|
* to correctly render a server.
|
|
|
|
*/
|
|
|
|
type LoadedServer = WithRelationships<Server, 'allocations' | 'user' | 'node'>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a server from the API and ensures that the allocations, user, and
|
|
|
|
* node data is loaded.
|
|
|
|
*/
|
|
|
|
export const getServer = async (id: number | string): Promise<LoadedServer> => {
|
|
|
|
const { data } = await http.get(`/api/application/servers/${id}`, {
|
|
|
|
params: {
|
2021-10-24 20:14:04 +00:00
|
|
|
include: [ 'allocations', 'user', 'node', 'variables' ],
|
2021-10-09 19:02:32 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-02-27 16:26:13 +00:00
|
|
|
return withRelationships(Transformers.toServer(data), 'allocations', 'user', 'node', 'variables');
|
2021-10-09 19:02:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an SWR instance by automatically loading in the server for the currently
|
|
|
|
* loaded route match in the admin area.
|
|
|
|
*/
|
|
|
|
export const useServerFromRoute = (): SWRResponse<LoadedServer, AxiosError> => {
|
|
|
|
const { params } = useRouteMatch<{ id: string }>();
|
|
|
|
|
|
|
|
return useSWR(`/api/application/servers/${params.id}`, async () => getServer(params.id), {
|
|
|
|
revalidateOnMount: false,
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
});
|
|
|
|
};
|