implement better HTTP request types handling,

add live updating of instances using statushash endpoint to reduce data transfer
This commit is contained in:
Arthur Lu 2023-07-11 21:14:54 +00:00
parent defc667041
commit 1dd32eda81
2 changed files with 22 additions and 3 deletions

View File

@ -4,6 +4,9 @@ import { PVE } from "../vars.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
let currentHash;
const refreshRate = 5000;
async function init () { async function init () {
setTitleAndHeader(); setTitleAndHeader();
const cookie = document.cookie; const cookie = document.cookie;
@ -11,13 +14,24 @@ async function init () {
goToPage("login.html"); goToPage("login.html");
} }
currentHash = (await requestAPI("/cluster/statushash")).data;
await populateInstances(); await populateInstances();
const addInstanceBtn = document.querySelector("#instance-add"); const addInstanceBtn = document.querySelector("#instance-add");
addInstanceBtn.addEventListener("click", handleInstanceAdd); addInstanceBtn.addEventListener("click", handleInstanceAdd);
window.setInterval(async () => {
const newHash = (await requestAPI("/cluster/statushash")).data;
if (currentHash !== newHash) {
currentHash = newHash;
populateInstances();
}
}, refreshRate);
} }
async function populateInstances () { async function populateInstances () {
const newHash = (await requestAPI("/cluster/statushash")).data;
currentHash = newHash;
const resources = await requestPVE("/cluster/resources", "GET"); const resources = await requestPVE("/cluster/resources", "GET");
const instanceContainer = document.getElementById("instance-container"); const instanceContainer = document.getElementById("instance-container");
const instances = []; const instances = [];

View File

@ -192,13 +192,18 @@ export async function requestAPI (path, method, body = null) {
async function request (url, content) { async function request (url, content) {
const response = await fetch(url, content); const response = await fetch(url, content);
const contentType = response.headers.get("Content-Type");
let data = null; let data = null;
try { if (contentType.includes("application/json")) {
data = await response.json(); data = await response.json();
data.status = response.status; data.status = response.status;
} }
catch { else if (contentType.includes("text/html")) {
data = null; data = { data: await response.text() };
data.status = response.status;
}
else {
data = response;
} }
if (!response.ok) { if (!response.ok) {