Source Code
The important bits that make this thing work
Background Monitor
apps/api/src/lib/status-monitor.ts (simplified)// Background Worker (API Container) - simplified async function performCheck() { const now = Date.now(); const checkedAt = new Date(now).toISOString(); let status = 'down'; let httpStatus = null; try { const res = await fetch(CHECK_URL, { timeout: 10000 }); httpStatus = res.status; // Treat any response < 500 as "up" (403 can be Cloudflare bot protection) if (httpStatus < 500) status = 'up'; } catch (err) { // timeouts / network errors count as "down" } // Cache current status and persist a snapshot (MongoDB) currentStatus = { status, checkedAt, lastCheckAt: now, httpStatus }; await saveSnapshot(currentStatus); } // Run immediately, then every 30s performCheck(); setInterval(performCheck, 30000);
The core monitoring logic lives in the API container. It polls downdetector.com every 30 seconds, caches the result, and persists snapshots for history. HTTP responses under 500 count as "up" because Cloudflare's bot detection returns 403s even when the site is operational.
Frontend Sync
apps/web/src/components/StatusDisplay.tsx (simplified)// Status checking - syncs with server's check time const CHECK_INTERVAL_MS = 30000; let lastServerCheck = null; function updateCountdown() { const countdownEl = document.getElementById('countdown'); if (!lastServerCheck) { countdownEl.textContent = 'Waiting...'; return; } const elapsed = Date.now() - lastServerCheck; const remaining = Math.max(0, Math.ceil( (CHECK_INTERVAL_MS - elapsed) / 1000 )); countdownEl.textContent = remaining === 0 ? 'Now...' : `${remaining}s`; } // Poll server every 5 seconds to stay in sync checkStatus(); setInterval(checkStatus, 5000); setInterval(updateCountdown, 1000);
The frontend polls
/api/v1/status every 5 seconds (lightweight) to get the server's last check timestamp. The countdown timer calculates time remaining based on when the server actually ran its check, keeping client and server in sync.