add api package cli arg,

fix comments
This commit is contained in:
Arthur Lu 2023-08-05 00:38:57 +00:00
parent 7b80445cf0
commit 491e492206
6 changed files with 23 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
**/package-lock.json **/package-lock.json
**/node_modules **/node_modules
**/localdb.json **/localdb.json
**/docs

View File

@ -12,11 +12,12 @@ import LocalDB from "./db.js";
import parseArgs from "minimist"; import parseArgs from "minimist";
global.argv = parseArgs(process.argv.slice(2), { global.argv = parseArgs(process.argv.slice(2), {
default: { default: {
package: "package.json",
localdb: "config/localdb.json" localdb: "config/localdb.json"
} }
}); });
global.api = api; global.api = api(global.argv.package);
global.pve = pve; global.pve = pve;
global.utils = utils; global.utils = utils;
global.db = new LocalDB(global.argv.localdb); global.db = new LocalDB(global.argv.localdb);
@ -29,7 +30,7 @@ app.use(cors({ origin: global.db.hostname }));
app.use(morgan("combined")); app.use(morgan("combined"));
global.server = app.listen(global.db.listenPort, () => { 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) => { import("./routes/auth.js").then((module) => {
@ -62,7 +63,7 @@ import("./routes/user.js").then((module) => {
* - 200: {version: string} * - 200: {version: string}
*/ */
app.get("/api/version", (req, res) => { app.get("/api/version", (req, res) => {
res.status(200).send({ version: api.version }); res.status(200).send({ version: global.api.version });
}); });
/** /**

View File

@ -1,2 +1,4 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
export default JSON.parse(readFileSync("package.json")); export default (path) => {
return JSON.parse(readFileSync(path));
};

View File

@ -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 {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} 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 * @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) { export async function requestPVE (path, method, cookies, body = null, token = null) {
const pveAPI = global.db.pveAPI; const pveAPI = global.db.pveAPI;

View File

@ -13,7 +13,7 @@ const pveAPIToken = global.db.pveAPIToken;
* POST - detach mounted disk from instance * POST - detach mounted disk from instance
* request: * request:
* - node: string - vm host node id * - node: string - vm host node id
* -y tpe: string - vm type (lxc, qemu) * - type: string - vm type (lxc, qemu)
* - vmid: number - vm id number * - vmid: number - vm id number
* - disk: string - disk id (sata0, NOT unused) * - disk: string - disk id (sata0, NOT unused)
* responses: * responses:

View File

@ -95,12 +95,24 @@ export async function approveResources (req, username, request) {
return approved; // if all requested resources pass, allow 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") { export function getObjectHash (object, alg = "sha256", format = "hex") {
const hash = createHash(alg); const hash = createHash(alg);
hash.update(JSON.stringify(object, Object.keys(object).sort())); hash.update(JSON.stringify(object, Object.keys(object).sort()));
return hash.digest(format); 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) { export function getTimeLeft (timeout) {
return Math.ceil((timeout._idleStart + timeout._idleTimeout - (global.process.uptime() * 1000))); return Math.ceil((timeout._idleStart + timeout._idleTimeout - (global.process.uptime() * 1000)));
} }