update to es6 and fix some syntax errors

This commit is contained in:
Arthur Lu 2023-04-26 01:18:05 +00:00
parent 631237d857
commit 2deda42ec5
5 changed files with 41 additions and 45 deletions

18
db.js
View File

@ -1,9 +1,9 @@
const fs = require("fs"); import { readFileSync, writeFileSync } from "fs";
template = "localdb.json.template" let template = "localdb.json.template"
filename = "localdb.json"; let filename = "localdb.json";
let db = JSON.parse(fs.readFileSync(template)); let db = JSON.parse(readFileSync(template));
try { try {
load(); load();
} }
@ -12,19 +12,17 @@ catch {
} }
function load () { function load () {
db = JSON.parse(fs.readFileSync(filename)); db = JSON.parse(readFileSync(filename));
} }
function save () { function save () {
fs.writeFileSync(filename, JSON.stringify(db)); writeFileSync(filename, JSON.stringify(db));
} }
function getResourceConfig() { export function getResourceConfig() {
return db.resources; return db.resources;
} }
function getUserConfig (username) { export function getUserConfig (username) {
return db.users[username]; return db.users[username];
} }
module.exports = {getUserConfig, getResourceConfig};

29
main.js
View File

@ -1,14 +1,15 @@
const express = require("express"); import express from "express";
const bodyParser = require("body-parser"); import bodyParser from "body-parser";
const cookieParser = require("cookie-parser") import cookieParser from "cookie-parser";
const cors = require("cors"); import cors from "cors";
const helmet = require("helmet"); import helmet from "helmet";
const morgan = require("morgan"); import morgan from "morgan";
var api = require("./package.json"); //import { version as _version } from "./package.json";
let api = {version: "0.0.1"};
const {pveAPIToken, listenPort, domain} = require("./vars.js"); import { pveAPIToken, listenPort, domain } from "./vars.js";
const {checkAuth, requestPVE, handleResponse, getDiskInfo} = require("./pve.js"); import { checkAuth, requestPVE, handleResponse, getDiskInfo } from "./pve.js";
const {getUserData, approveResources} = require("./utils.js") import { getUserData, approveResources } from "./utils.js";
const app = express(); const app = express();
app.use(helmet()); app.use(helmet());
@ -19,7 +20,7 @@ app.use(morgan("combined"));
app.get("/api/version", (req, res) => { app.get("/api/version", (req, res) => {
res.status(200).send({version: api.version}); res.status(200).send({version: _version});
}); });
app.get("/api/echo", (req, res) => { app.get("/api/echo", (req, res) => {
@ -32,13 +33,13 @@ app.get("/api/auth", async (req, res) => {
}); });
app.get("/api/proxmox/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token app.get("/api/proxmox/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token
path = req.url.replace("/api/proxmox", ""); let path = req.url.replace("/api/proxmox", "");
let result = await requestPVE(path, "GET", req.cookies); let result = await requestPVE(path, "GET", req.cookies);
res.status(result.status).send(result.data); res.status(result.status).send(result.data);
}); });
app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token
path = req.url.replace("/api/proxmox", ""); let path = req.url.replace("/api/proxmox", "");
let result = await requestPVE(path, "POST", req.cookies, JSON.stringify(req.body)); // need to stringify body because of other issues let result = await requestPVE(path, "POST", req.cookies, JSON.stringify(req.body)); // need to stringify body because of other issues
res.status(result.status).send(result.data); res.status(result.status).send(result.data);
}); });
@ -235,7 +236,7 @@ app.post("/api/instance", async (req, res) => {
memory: req.body.memory, memory: req.body.memory,
pool: user.instances.pool pool: user.instances.pool
}; };
for (key of Object.keys(user.instances.templates[req.body.type])) { for (let key of Object.keys(user.instances.templates[req.body.type])) {
action[key] = user.instances.templates[req.body.type][key]; action[key] = user.instances.templates[req.body.type][key];
} }
if (req.body.type === "lxc") { if (req.body.type === "lxc") {

View File

@ -3,6 +3,7 @@
"version": "0.0.1", "version": "0.0.1",
"description": "REST API for ProxmoxAAS", "description": "REST API for ProxmoxAAS",
"main": "main.js", "main": "main.js",
"type": "module",
"dependencies": { "dependencies": {
"axios": "^1.3.2", "axios": "^1.3.2",
"body-parser": "^1.20.1", "body-parser": "^1.20.1",

20
pve.js
View File

@ -1,7 +1,7 @@
const axios = require('axios'); import axios from 'axios';
const {pveAPI, pveAPIToken} = require("./vars.js"); import { pveAPI, pveAPIToken } from "./vars.js";
async function checkAuth (cookies, res, vmpath = null) { export async function checkAuth (cookies, res, vmpath = null) {
let auth = false; let auth = false;
if (vmpath) { if (vmpath) {
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies); let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
@ -18,7 +18,7 @@ async function checkAuth (cookies, res, vmpath = null) {
} }
} }
async function requestPVE (path, method, cookies, body = null, token = null) { export async function requestPVE (path, method, cookies, body = null, token = null) {
let url = `${pveAPI}${path}`; let url = `${pveAPI}${path}`;
let content = { let content = {
method: method, method: method,
@ -50,7 +50,7 @@ async function requestPVE (path, method, cookies, body = null, token = null) {
} }
} }
async function handleResponse (node, result, res) { export async function handleResponse (node, result, res) {
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
if (result.data.data) { if (result.data.data) {
let upid = result.data.data; let upid = result.data.data;
@ -78,7 +78,7 @@ async function handleResponse (node, result, res) {
} }
} }
async function getUsedResources (req, resourceMeta) { export async function getUsedResources (req, resourceMeta) {
let response = await requestPVE("/cluster/resources", "GET", req.cookies); let response = await requestPVE("/cluster/resources", "GET", req.cookies);
let used = {}; let used = {};
let diskprefixes = []; let diskprefixes = [];
@ -90,11 +90,11 @@ async function getUsedResources (req, resourceMeta) {
} }
} }
} }
for (instance of response.data.data) { for (let instance of response.data.data) {
if (instance.type === "lxc" || instance.type === "qemu") { if (instance.type === "lxc" || instance.type === "qemu") {
let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies); let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies);
config = config.data.data; config = config.data.data;
for (key of Object.keys(config)) { for (let key of Object.keys(config)) {
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") { if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
used[key] += config[key]; used[key] += config[key];
} }
@ -108,7 +108,7 @@ async function getUsedResources (req, resourceMeta) {
return used; return used;
} }
async function getDiskInfo (node, type, vmid, disk) { export async function getDiskInfo (node, type, vmid, disk) {
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken); let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
let storageID = config.data.data[disk].split(":")[0]; let storageID = config.data.data[disk].split(":")[0];
let volID = config.data.data[disk].split(",")[0]; let volID = config.data.data[disk].split(",")[0];
@ -116,5 +116,3 @@ async function getDiskInfo (node, type, vmid, disk) {
volInfo.data.data.storage = storageID; volInfo.data.data.storage = storageID;
return volInfo.data.data; return volInfo.data.data;
} }
module.exports = {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo};

View File

@ -1,7 +1,7 @@
const {getUsedResources} = require("./pve.js"); import { getUsedResources } from "./pve.js";
const {getUserConfig, getResourceConfig} = require("./db.js"); import { getUserConfig, getResourceConfig } from "./db.js";
async function getUserData (req, username) { export async function getUserData (req, username) {
let resources = await getAllocatedResources(req, username); let resources = await getAllocatedResources(req, username);
let instances = getUserConfig(req.cookies.username).instances; let instances = getUserConfig(req.cookies.username).instances;
return {resources: resources, instances: instances}; return {resources: resources, instances: instances};
@ -11,14 +11,14 @@ async function getAllocatedResources (req, username) {
let dbResources = getResourceConfig(); let dbResources = getResourceConfig();
let used = await getUsedResources(req, dbResources); let used = await getUsedResources(req, dbResources);
let max = getUserConfig(username).resources.max; let max = getUserConfig(username).resources.max;
avail = {}; let avail = {};
Object.keys(max).forEach((k) => { Object.keys(max).forEach((k) => {
avail[k] = max[k] - used[k]; avail[k] = max[k] - used[k];
}); });
return {used: used, max: max, avail: avail, units: dbResources}; return {used: used, max: max, avail: avail, units: dbResources};
} }
async function approveResources (req, username, request) { export async function approveResources (req, username, request) {
let avail = (await getAllocatedResources(req, username)).avail; let avail = (await getAllocatedResources(req, username)).avail;
@ -33,5 +33,3 @@ async function approveResources (req, username, request) {
}); });
return approved; return approved;
} }
module.exports = {getUserData, approveResources}