feat: add /stats endpoint to retrieve server and system statistics
Browse files- src/index.js +147 -0
src/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import express from 'express';
|
|
|
|
|
|
|
| 2 |
|
| 3 |
const app = express();
|
| 4 |
const PORT = process.env.PORT || 7860;
|
|
@@ -7,6 +9,151 @@ app.get('/', (req, res) => {
|
|
| 7 |
res.send('Hello, This is LumaKit Backend!');
|
| 8 |
});
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app.listen(PORT, '0.0.0.0', () => {
|
| 11 |
console.log(`Server is running on http://0.0.0.0:${PORT}`);
|
| 12 |
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import os from 'os';
|
| 3 |
+
import { promises as fs } from 'fs';
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
const PORT = process.env.PORT || 7860;
|
|
|
|
| 9 |
res.send('Hello, This is LumaKit Backend!');
|
| 10 |
});
|
| 11 |
|
| 12 |
+
app.get('/health', (req, res) => {
|
| 13 |
+
res.status(200).json({ status: 'ok' });
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
app.get("/stats", async (req, res) => {
|
| 17 |
+
try {
|
| 18 |
+
// Get network interfaces
|
| 19 |
+
const networkInterfaces = os.networkInterfaces();
|
| 20 |
+
const ipAddresses = [];
|
| 21 |
+
|
| 22 |
+
for (const [name, interfaces] of Object.entries(networkInterfaces)) {
|
| 23 |
+
for (const iface of interfaces) {
|
| 24 |
+
if (!iface.internal) {
|
| 25 |
+
ipAddresses.push({
|
| 26 |
+
interface: name,
|
| 27 |
+
address: iface.address,
|
| 28 |
+
family: iface.family,
|
| 29 |
+
mac: iface.mac
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Get memory information
|
| 36 |
+
const totalMemory = os.totalmem();
|
| 37 |
+
const freeMemory = os.freemem();
|
| 38 |
+
const usedMemory = totalMemory - freeMemory;
|
| 39 |
+
|
| 40 |
+
// Get CPU information
|
| 41 |
+
const cpus = os.cpus();
|
| 42 |
+
const loadAverage = os.loadavg();
|
| 43 |
+
|
| 44 |
+
// Get uptime
|
| 45 |
+
const systemUptime = os.uptime();
|
| 46 |
+
const processUptime = process.uptime();
|
| 47 |
+
|
| 48 |
+
// Get platform and architecture
|
| 49 |
+
const platform = os.platform();
|
| 50 |
+
const architecture = os.arch();
|
| 51 |
+
const release = os.release();
|
| 52 |
+
const hostname = os.hostname();
|
| 53 |
+
|
| 54 |
+
// Get Node.js/Bun version and environment
|
| 55 |
+
const nodeVersion = process.version;
|
| 56 |
+
const bunVersion = process.versions?.bun || 'N/A';
|
| 57 |
+
|
| 58 |
+
// Try to get container information if available
|
| 59 |
+
let containerInfo = null;
|
| 60 |
+
try {
|
| 61 |
+
const cgroupData = await fs.readFile('/proc/1/cgroup', 'utf8').catch(() => null);
|
| 62 |
+
if (cgroupData && cgroupData.includes('docker')) {
|
| 63 |
+
containerInfo = { type: 'docker', detected: true };
|
| 64 |
+
} else if (cgroupData && cgroupData.includes('kubepods')) {
|
| 65 |
+
containerInfo = { type: 'kubernetes', detected: true };
|
| 66 |
+
}
|
| 67 |
+
} catch (error) {
|
| 68 |
+
containerInfo = { type: 'unknown', detected: false };
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// Get disk usage for the current directory
|
| 72 |
+
let diskUsage = null;
|
| 73 |
+
try {
|
| 74 |
+
const stats = await fs.stat(process.cwd());
|
| 75 |
+
diskUsage = {
|
| 76 |
+
currentDirectory: process.cwd(),
|
| 77 |
+
accessible: true
|
| 78 |
+
};
|
| 79 |
+
} catch (error) {
|
| 80 |
+
diskUsage = {
|
| 81 |
+
currentDirectory: process.cwd(),
|
| 82 |
+
accessible: false,
|
| 83 |
+
error: error.message
|
| 84 |
+
};
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
const serverStats = {
|
| 88 |
+
timestamp: new Date().toISOString(),
|
| 89 |
+
server: {
|
| 90 |
+
name: 'LumaKit Backend',
|
| 91 |
+
version: require('../package.json').version,
|
| 92 |
+
environment: process.env.NODE_ENV || 'development',
|
| 93 |
+
port: PORT,
|
| 94 |
+
pid: process.pid,
|
| 95 |
+
uptime: {
|
| 96 |
+
process: Math.floor(processUptime),
|
| 97 |
+
system: Math.floor(systemUptime)
|
| 98 |
+
}
|
| 99 |
+
},
|
| 100 |
+
machine: {
|
| 101 |
+
hostname: hostname,
|
| 102 |
+
platform: platform,
|
| 103 |
+
architecture: architecture,
|
| 104 |
+
release: release,
|
| 105 |
+
endianness: os.endianness(),
|
| 106 |
+
tmpdir: os.tmpdir(),
|
| 107 |
+
homedir: os.homedir()
|
| 108 |
+
},
|
| 109 |
+
network: {
|
| 110 |
+
ipAddresses: ipAddresses,
|
| 111 |
+
hostname: hostname
|
| 112 |
+
},
|
| 113 |
+
runtime: {
|
| 114 |
+
node: nodeVersion,
|
| 115 |
+
bun: bunVersion,
|
| 116 |
+
engine: bunVersion !== 'N/A' ? 'bun' : 'node'
|
| 117 |
+
},
|
| 118 |
+
memory: {
|
| 119 |
+
total: Math.round(totalMemory / 1024 / 1024),
|
| 120 |
+
free: Math.round(freeMemory / 1024 / 1024),
|
| 121 |
+
used: Math.round(usedMemory / 1024 / 1024),
|
| 122 |
+
percentage: Math.round((usedMemory / totalMemory) * 100),
|
| 123 |
+
unit: 'MB'
|
| 124 |
+
},
|
| 125 |
+
cpu: {
|
| 126 |
+
count: cpus.length,
|
| 127 |
+
model: cpus[0]?.model || 'Unknown',
|
| 128 |
+
speed: cpus[0]?.speed || 0,
|
| 129 |
+
loadAverage: {
|
| 130 |
+
'1min': loadAverage[0]?.toFixed(2) || 0,
|
| 131 |
+
'5min': loadAverage[1]?.toFixed(2) || 0,
|
| 132 |
+
'15min': loadAverage[2]?.toFixed(2) || 0
|
| 133 |
+
}
|
| 134 |
+
},
|
| 135 |
+
container: containerInfo,
|
| 136 |
+
disk: diskUsage,
|
| 137 |
+
environment: {
|
| 138 |
+
nodeEnv: process.env.NODE_ENV,
|
| 139 |
+
port: process.env.PORT,
|
| 140 |
+
pwd: process.env.PWD,
|
| 141 |
+
user: process.env.USER || process.env.USERNAME,
|
| 142 |
+
shell: process.env.SHELL,
|
| 143 |
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
| 144 |
+
}
|
| 145 |
+
};
|
| 146 |
+
|
| 147 |
+
res.status(200).json(serverStats);
|
| 148 |
+
} catch (error) {
|
| 149 |
+
res.status(500).json({
|
| 150 |
+
error: 'Failed to retrieve server stats',
|
| 151 |
+
message: error.message,
|
| 152 |
+
timestamp: new Date().toISOString()
|
| 153 |
+
});
|
| 154 |
+
}
|
| 155 |
+
});
|
| 156 |
+
|
| 157 |
app.listen(PORT, '0.0.0.0', () => {
|
| 158 |
console.log(`Server is running on http://0.0.0.0:${PORT}`);
|
| 159 |
});
|