separate user data endpoints

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-12 20:51:07 +00:00
parent fd969e04af
commit f21d3cffea
4 changed files with 25 additions and 13 deletions

2
db.js
View File

@ -19,7 +19,7 @@ function save () {
writeFileSync(filename, JSON.stringify(db)); writeFileSync(filename, JSON.stringify(db));
} }
export function getResourceConfig() { export function getResourceConfig () {
return db.resources; return db.resources;
} }

View File

@ -37,6 +37,10 @@
"cephpl": 1099511627776 "cephpl": 1099511627776
} }
}, },
"nodes": [
"nodeid-1",
"nodeid-2"
],
"instances": { "instances": {
"vmid": { "vmid": {
"min": 100, "min": 100,

24
main.js
View File

@ -8,7 +8,8 @@ import api from "./package.json" assert {type: "json"};
import { pveAPIToken, listenPort, domain } from "./vars.js"; import { pveAPIToken, listenPort, domain } from "./vars.js";
import { checkAuth, requestPVE, handleResponse, getDiskInfo } from "./pve.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(); const app = express();
app.use(helmet()); 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); res.status(result.status).send(result.data);
}); });
app.get("/api/user", async (req, res) => { app.get("/api/user/resources", async (req, res) => {
// check auth // check auth
await checkAuth(req.cookies, res); await checkAuth(req.cookies, res);
res.status(200).send(await getUserData(req, req.cookies.username)); let resources = await getAllocatedResources(req, req.cookies.username);
res.end(); res.status(200).send(resources);
return;
}); });
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) => { app.post("/api/instance/disk/detach", async (req, res) => {
// check auth // check auth
await checkAuth(req.cookies, res); await checkAuth(req.cookies, res);

View File

@ -1,13 +1,7 @@
import { getUsedResources } from "./pve.js"; import { getUsedResources } from "./pve.js";
import { getUserConfig, getResourceConfig } from "./db.js"; import { getUserConfig, getResourceConfig } from "./db.js";
export async function getUserData (req, username) { export async function getAllocatedResources (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) {
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;