[FEATURE] Migrated to new loading sequence (#6)
Co-authored-by: Dunemask <dunemask@gmail.com> Reviewed-on: https://gitea.dunemask.dev/elysium/minecluster/pulls/6
This commit is contained in:
parent
fb57c03ba7
commit
6eb4ed3e95
53 changed files with 1349 additions and 449 deletions
|
@ -7,35 +7,122 @@ const asExpressClientError = (e) => {
|
|||
throw new ExpressClientError({ m: e.message, c: 409 });
|
||||
};
|
||||
|
||||
const getMclName = (host, id) => `${host.replaceAll(".", "-")}-${id}`;
|
||||
|
||||
export async function createServerEntry(serverSpec) {
|
||||
const { name, host, version, serverType: server_type, memory } = serverSpec;
|
||||
const q = insertQuery(table, { name, host, version, server_type, memory });
|
||||
const {
|
||||
name,
|
||||
host,
|
||||
version,
|
||||
serverType: server_type,
|
||||
memory,
|
||||
backupHost: backup_host,
|
||||
backupBucket: backup_bucket_path,
|
||||
backupId: backup_id,
|
||||
backupKey: backup_key,
|
||||
backupInterval: backup_interval,
|
||||
} = serverSpec;
|
||||
var q = insertQuery(table, {
|
||||
name,
|
||||
host,
|
||||
version,
|
||||
server_type,
|
||||
memory,
|
||||
backup_enabled: !!backup_interval, // We already verified the payload, so any backup key will work
|
||||
backup_host,
|
||||
backup_bucket_path,
|
||||
backup_id,
|
||||
backup_key,
|
||||
backup_interval,
|
||||
});
|
||||
q += "\n RETURNING *";
|
||||
try {
|
||||
const entries = await pg.query(q);
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
host,
|
||||
version,
|
||||
server_type: serverType,
|
||||
memory,
|
||||
backup_enabled: backupEnabled,
|
||||
backup_host: backupHost,
|
||||
backup_bucket_path: backupPath,
|
||||
backup_id: backupId,
|
||||
backup_key: backupKey,
|
||||
backup_interval: backupInterval,
|
||||
} = entries[0];
|
||||
const mclName = getMclName(host, id);
|
||||
return {
|
||||
name,
|
||||
mclName,
|
||||
id,
|
||||
host,
|
||||
version,
|
||||
serverType,
|
||||
memory,
|
||||
backupEnabled,
|
||||
backupHost,
|
||||
backupPath,
|
||||
backupId,
|
||||
backupKey,
|
||||
backupInterval,
|
||||
};
|
||||
} catch (e) {
|
||||
asExpressClientError(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteServerEntry(serverId) {
|
||||
if (!serverId) asExpressClientError({ message: "Server ID Required!" });
|
||||
const q = deleteQuery(table, { id: serverId });
|
||||
return pg.query(q).catch(asExpressClientError);
|
||||
}
|
||||
|
||||
export async function deleteServerEntry(serverName) {
|
||||
if (!serverName) asExpressClientError({ message: "Server Name Required!" });
|
||||
const q = deleteQuery(table, { name: serverName });
|
||||
return pg.query(q).catch(asExpressClientError);
|
||||
}
|
||||
|
||||
export async function getServerEntry(serverName) {
|
||||
if (!serverName) asExpressClientError({ message: "Server Name Required!" });
|
||||
const q = selectWhereQuery(table, { name: serverName });
|
||||
export async function getServerEntry(serverId) {
|
||||
if (!serverId) asExpressClientError({ message: "Server ID Required!" });
|
||||
const q = selectWhereQuery(table, { id: serverId });
|
||||
try {
|
||||
const serverSpecs = await pg.query(q);
|
||||
if (serverSpecs.length === 0) return [];
|
||||
if (!serverSpecs.length === 1)
|
||||
throw Error("Multiple servers found with the same name!");
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
host,
|
||||
version,
|
||||
server_type: serverType,
|
||||
memory,
|
||||
backup_enabled: backupEnabled,
|
||||
backup_host: backupHost,
|
||||
backup_bucket_path: backupPath,
|
||||
backup_id: backupId,
|
||||
backup_key: backupKey,
|
||||
backup_interval: backupInterval,
|
||||
} = serverSpecs[0];
|
||||
return { name, host, version, serverType, memory };
|
||||
const mclName = getMclName(host, id);
|
||||
return {
|
||||
name,
|
||||
mclName,
|
||||
id,
|
||||
host,
|
||||
version,
|
||||
serverType,
|
||||
memory,
|
||||
backupEnabled,
|
||||
backupHost,
|
||||
backupPath,
|
||||
backupId,
|
||||
backupKey,
|
||||
backupInterval,
|
||||
};
|
||||
} catch (e) {
|
||||
asExpressClientError(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getServerEntries() {
|
||||
const q = `SELECT * FROM ${table}`;
|
||||
return pg.query(q);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue