diff --git a/db.js b/db.js index a34bc4d..7fe4ccd 100644 --- a/db.js +++ b/db.js @@ -19,7 +19,7 @@ function save () { writeFileSync(filename, JSON.stringify(db)); } -export function getResourceConfig() { +export function getResourceConfig () { return db.resources; } diff --git a/localdb.json.template b/localdb.json.template index d790382..2021e31 100644 --- a/localdb.json.template +++ b/localdb.json.template @@ -37,6 +37,10 @@ "cephpl": 1099511627776 } }, + "nodes": [ + "nodeid-1", + "nodeid-2" + ], "instances": { "vmid": { "min": 100, diff --git a/main.js b/main.js index 4b68ad6..02a32f7 100644 --- a/main.js +++ b/main.js @@ -8,7 +8,8 @@ import api from "./package.json" assert {type: "json"}; import { pveAPIToken, listenPort, domain } from "./vars.js"; import { checkAuth, requestPVE, handleResponse, getDiskInfo } from "./pve.js"; -import { getUserData, approveResources } from "./utils.js"; +import { getAllocatedResources, approveResources } from "./utils.js"; +import { getUserConfig } from "./db.js"; const app = express(); app.use(helmet()); @@ -42,14 +43,27 @@ app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST prox res.status(result.status).send(result.data); }); -app.get("/api/user", async (req, res) => { +app.get("/api/user/resources", async (req, res) => { // check auth await checkAuth(req.cookies, res); - res.status(200).send(await getUserData(req, req.cookies.username)); - res.end(); - return; + let resources = await getAllocatedResources(req, req.cookies.username); + res.status(200).send(resources); }); +app.get("/api/user/instances", async (req, res) => { + // check auth + await checkAuth(req.cookies, res); + let config = getUserConfig(req.cookies.username); + res.status(200).send(config.instances) +}); + +app.get("/api/user/nodes", async (req, res) => { + // check auth + await checkAuth(req.cookies, res); + let config = getUserConfig(req.cookies.username); + res.status(200).send(config.nodes) +}) + app.post("/api/instance/disk/detach", async (req, res) => { // check auth await checkAuth(req.cookies, res); diff --git a/utils.js b/utils.js index 8710140..c1a9176 100644 --- a/utils.js +++ b/utils.js @@ -1,13 +1,7 @@ import { getUsedResources } from "./pve.js"; import { getUserConfig, getResourceConfig } from "./db.js"; -export async function getUserData (req, username) { - let resources = await getAllocatedResources(req, username); - let user = getUserConfig(req.cookies.username); - return {resources: resources, instances: user.instances, nodes: user.nodes}; -} - -async function getAllocatedResources (req, username) { +export async function getAllocatedResources (req, username) { let dbResources = getResourceConfig(); let used = await getUsedResources(req, dbResources); let max = getUserConfig(username).resources.max;