From e0b4c4e963afe23525b146883f70b6cb879c2297 Mon Sep 17 00:00:00 2001 From: Minteck Date: Mon, 30 May 2022 10:01:09 +0200 Subject: Improve performance (especially for Windows Server) --- index.ts | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'index.ts') diff --git a/index.ts b/index.ts index 00d399b..fcc6846 100644 --- a/index.ts +++ b/index.ts @@ -1,19 +1,31 @@ import express from "express"; import si from "systeminformation"; +import nodeOS from "os"; const app = express(); +const PORT = 52937; -app.get("/json", async (req, res) => { +async function refresh() { // Gives the info for this server - let cpu = await si.cpu(); - let cpuSpeed = await si.cpuCurrentSpeed(); - let temp = await si.cpuTemperature(); - let memory = await si.mem(); - let os = await si.osInfo(); - let time = si.time(); - let load = await si.currentLoad(); - let processes = await si.processes(); + global.cpu = await si.cpu(); + global.cpuSpeed = await si.cpuCurrentSpeed(); + global.temp = await si.cpuTemperature(); + global.memory = await si.mem(); + global.os = await si.osInfo(); + global.time = si.time(); + global.load = await si.currentLoad(); + global.processes = await si.processes(); +} +app.get("/json", async (req, res) => { + let cpu = global.cpu; + let cpuSpeed = global.cpuSpeed; + let temp = global.temp; + let memory = global.memory; + let os = global.os; + let time = global.time; + let load = global.load; + let processes = global.processes; let uptime = parseInt(time.uptime) * 1000; res.json({ @@ -44,14 +56,26 @@ app.get("/json", async (req, res) => { total: memory.total }, os: { - name: os.distro, + name: nodeOS.platform() === "win32" + ? os.platform + : os.distro, version: os.release }, uptime: uptime, - version: "1.0.0" + version: "1.1.0" }); }); -app.listen(52937, () => { - console.log("listening!"); -}); \ No newline at end of file +console.log("Waiting for initial data processing..."); +refresh().then(() => { + app.listen(PORT, () => { + console.log("Listening on port " + PORT + "!"); + setInterval(async () => { + await refresh(); + }, ( + nodeOS.platform() === "win32" + ? 300000 + : 3000 + )) + }); +}) \ No newline at end of file -- cgit