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();