feat(posimai-dev): add /api/health endpoint with CORS
Returns cpu_pct, mem_used_mb, mem_total_mb, uptime_s, active_sessions, hostname, node_version, platform, timestamp. Enables Atlas and other Tailscale-accessible clients to pull realtime Ubuntu PC metrics. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f57125c5b6
commit
c09d5defd3
|
|
@ -37,6 +37,35 @@ app.get('/api/sessions/:id', (req, res) => {
|
||||||
res.type('text/plain').send(fs.readFileSync(file, 'utf8'));
|
res.type('text/plain').send(fs.readFileSync(file, 'utf8'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── ヘルス & メトリクス API (/api/health) ──────────────────────
|
||||||
|
// Atlas など外部から参照される。CORS ヘッダーを付与して Vercel 上の Atlas からも取得可能にする
|
||||||
|
app.get('/api/health', (req, res) => {
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
|
||||||
|
const mem = os.freemem();
|
||||||
|
const total = os.totalmem();
|
||||||
|
const cpus = os.cpus();
|
||||||
|
|
||||||
|
// CPU 使用率: 全コアの平均(起動時 idle から差し引く簡易計算)
|
||||||
|
const cpuUsage = cpus.reduce((sum, c) => {
|
||||||
|
const t = Object.values(c.times).reduce((a, b) => a + b, 0);
|
||||||
|
return sum + ((t - c.times.idle) / t) * 100;
|
||||||
|
}, 0) / cpus.length;
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
ok: true,
|
||||||
|
hostname: os.hostname(),
|
||||||
|
uptime_s: Math.floor(os.uptime()),
|
||||||
|
cpu_pct: Math.round(cpuUsage),
|
||||||
|
mem_used_mb: Math.round((total - mem) / 1024 / 1024),
|
||||||
|
mem_total_mb: Math.round(total / 1024 / 1024),
|
||||||
|
active_sessions: wss.clients ? wss.clients.size : 0,
|
||||||
|
node_version: process.version,
|
||||||
|
platform: os.platform(),
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Tailscale証明書を自動検出
|
// Tailscale証明書を自動検出
|
||||||
function findCert() {
|
function findCert() {
|
||||||
const home = os.homedir();
|
const home = os.homedir();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue