From 491e49220680595c0f03759eb41ae495eb8a4db5 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Sat, 5 Aug 2023 00:38:57 +0000 Subject: [PATCH] add api package cli arg, fix comments --- .gitignore | 3 ++- src/main.js | 7 ++++--- src/package.js | 4 +++- src/pve.js | 2 +- src/routes/cluster/disk.js | 2 +- src/utils.js | 12 ++++++++++++ 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 0dbb217..b727747 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ **/package-lock.json **/node_modules -**/localdb.json \ No newline at end of file +**/localdb.json +**/docs \ No newline at end of file diff --git a/src/main.js b/src/main.js index b5d4eef..04e99e9 100644 --- a/src/main.js +++ b/src/main.js @@ -12,11 +12,12 @@ import LocalDB from "./db.js"; import parseArgs from "minimist"; global.argv = parseArgs(process.argv.slice(2), { default: { + package: "package.json", localdb: "config/localdb.json" } }); -global.api = api; +global.api = api(global.argv.package); global.pve = pve; global.utils = utils; global.db = new LocalDB(global.argv.localdb); @@ -29,7 +30,7 @@ app.use(cors({ origin: global.db.hostname })); app.use(morgan("combined")); global.server = app.listen(global.db.listenPort, () => { - console.log(`proxmoxaas-api v${api.version} listening on port ${global.db.listenPort}`); + console.log(`proxmoxaas-api v${global.api.version} listening on port ${global.db.listenPort}`); }); import("./routes/auth.js").then((module) => { @@ -62,7 +63,7 @@ import("./routes/user.js").then((module) => { * - 200: {version: string} */ app.get("/api/version", (req, res) => { - res.status(200).send({ version: api.version }); + res.status(200).send({ version: global.api.version }); }); /** diff --git a/src/package.js b/src/package.js index 036656e..0fc84d3 100644 --- a/src/package.js +++ b/src/package.js @@ -1,2 +1,4 @@ import { readFileSync } from "fs"; -export default JSON.parse(readFileSync("package.json")); +export default (path) => { + return JSON.parse(readFileSync(path)); +}; diff --git a/src/pve.js b/src/pve.js index 3e10a27..9c3e8f1 100644 --- a/src/pve.js +++ b/src/pve.js @@ -7,7 +7,7 @@ import axios from "axios"; * @param {Object} cookies user cookies for authorization if an API token is not used. Safest option for authentication. * @param {string} body body parameters and data to be sent. Optional. * @param {string} token proxmox API token to be used for controled priviledge elevation, allows user requests to perform admin actions safely. Optional - * @returns {Obejct} HTTP response object or HTTP error object + * @returns {Object} HTTP response object or HTTP error object */ export async function requestPVE (path, method, cookies, body = null, token = null) { const pveAPI = global.db.pveAPI; diff --git a/src/routes/cluster/disk.js b/src/routes/cluster/disk.js index 43bb51e..8cac8f0 100644 --- a/src/routes/cluster/disk.js +++ b/src/routes/cluster/disk.js @@ -13,7 +13,7 @@ const pveAPIToken = global.db.pveAPIToken; * POST - detach mounted disk from instance * request: * - node: string - vm host node id - * -y tpe: string - vm type (lxc, qemu) + * - type: string - vm type (lxc, qemu) * - vmid: number - vm id number * - disk: string - disk id (sata0, NOT unused) * responses: diff --git a/src/utils.js b/src/utils.js index eca8251..e2ac37a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -95,12 +95,24 @@ export async function approveResources (req, username, request) { return approved; // if all requested resources pass, allow } +/** + * Get the hash value of an object with data values. + * @param {Object} object to be hashed. + * @param {string} alg algorithm used to get digest. + * @param {string} format format of digest. + * @returns {string} digest of hash function. + */ export function getObjectHash (object, alg = "sha256", format = "hex") { const hash = createHash(alg); hash.update(JSON.stringify(object, Object.keys(object).sort())); return hash.digest(format); } +/** + * Get the time remaining of scheduler timeout object. + * @param {Object} timeout object to get time reamining. + * @returns {number} milliseconds remaining until next event. + */ export function getTimeLeft (timeout) { return Math.ceil((timeout._idleStart + timeout._idleTimeout - (global.process.uptime() * 1000))); }