Files
puter/extensions/serverInfo.ts
Daniel Salazar 594cbe03ef tests: big test push with lots of missing areas (#3067)
- Strengthen Broadcast tests
- strengthen oidc tests
- add middleware tests
- add extensions test
- add puter hosting tests
- add Chat Completion Driver tests
2026-05-10 15:06:48 -07:00

74 lines
2.0 KiB
TypeScript

import type { Request, Response } from 'express';
import { extension } from '@heyputer/backend/src/extensions';
import fs from 'fs/promises';
import os from 'os';
export const handleServerInfo = async (
_req: Request,
res: Response,
): Promise<void> => {
const osData = {
platform: os.platform(),
type: os.type(),
release: os.release(),
pretty: `${os.type()} ${os.release()}`,
};
const cpus = os.cpus();
const cpuData = {
model: cpus[0]?.model || 'Unknown',
cores: cpus.length,
};
const ramData = {
total: os.totalmem(),
free: os.freemem(),
totalGB: (os.totalmem() / 1073741824).toFixed(2),
freeGB: (os.freemem() / 1073741824).toFixed(2),
};
const uptimeSeconds = os.uptime();
const uptimeData = {
seconds: uptimeSeconds,
days: Math.floor(uptimeSeconds / 86400),
hours: Math.floor((uptimeSeconds % 86400) / 3600),
minutes: Math.floor((uptimeSeconds % 3600) / 60),
pretty: `${Math.floor(uptimeSeconds / 86400)}d ${Math.floor((uptimeSeconds % 86400) / 3600)}h ${Math.floor((uptimeSeconds % 3600) / 60)}m`,
};
let diskData: Record<string, string> = {
total: 'N/A',
free: 'N/A',
used: 'N/A',
};
try {
const stats = await fs.statfs('/');
const totalGB = (stats.blocks * stats.bsize) / 1073741824;
const freeGB = (stats.bfree * stats.bsize) / 1073741824;
const usedGB = (totalGB - freeGB).toFixed(2);
diskData = {
total: totalGB.toFixed(2),
free: freeGB.toFixed(2),
used: usedGB,
};
} catch (err) {
console.error('Disk stats error:', err);
}
res.json({
os: osData,
cpu: cpuData,
ram: ramData,
uptime: uptimeData,
disk: diskData,
loadavg: os.loadavg(),
hostname: os.hostname(),
});
};
extension.get(
'/serverInfo',
{ subdomain: 'api', adminOnly: true },
handleServerInfo,
);