From c09d5defd3a26d76ac7b60b8ee82716cc9a5d6e7 Mon Sep 17 00:00:00 2001 From: posimai Date: Tue, 31 Mar 2026 07:48:01 +0900 Subject: [PATCH] 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 --- posimai-dev/server.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/posimai-dev/server.js b/posimai-dev/server.js index 22a38726..2f660c30 100644 --- a/posimai-dev/server.js +++ b/posimai-dev/server.js @@ -37,6 +37,35 @@ app.get('/api/sessions/:id', (req, res) => { 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証明書を自動検出 function findCert() { const home = os.homedir();