Files
events-hub/backend/src/index.js
T
joshuaandClaude Opus 5.5 041461f2b5 Servers and the agent, backend (2.1.0): join, jobs, schedules, port list, stats, alerts
- Agent API /api/agent/v1: join with a one-time token, then poll, stats,
  report and export upload, all signed with the server's Ed25519 key.
- Jobs signed with a separate job key (HUB_JOB_SIGNING_KEY_FILE) when
  delivered, config sealed to the server; one at a time per server;
  missed when not picked up in their window.
- New install (licence, ports, preflight against the release's env
  template), Proxy done with public URL checks, scheduled / emergency
  (2FA, reason) / batch updates with email and banner, uninstall with
  an encrypted export first (the key is never stored), adopt and
  linking existing installs, purge after 30 days.
- Port list, install defaults, server configuration (encrypted), stats
  per minute / hour with roll-up, server alerts. contracts 1.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:12:28 +02:00

58 lines
2.7 KiB
JavaScript

require('dotenv').config();
const required = ['DATABASE_URL', 'JWT_SECRET', 'HUB_ENCRYPTION_KEY'];
const missing = required.filter(k => !process.env[k]);
if (missing.length) {
console.error(`[hub] Missing environment variables: ${missing.join(', ')} (see .env.example)`);
process.exit(1);
}
const app = require('./app');
const { keyPair } = require('./utils/licenceToken');
const { runAlerts, rollupHeartbeats } = require('./utils/alerts');
const { importSmtpFromEnv } = require('./utils/settings');
const { syncReleases } = require('./utils/releaseSync');
const { runBilling } = require('./utils/billing');
const servers = require('./utils/servers');
const jobs = require('./utils/jobs');
// The job-signing key (2.1.0) is optional until the first server joins; warn if it's missing.
try {
servers.jobSigningKey();
} catch (e) {
console.warn(`[hub] ${e.message} Servers can't join until it's set.`);
}
// Fail early if the licence signing key is missing or wrong.
try {
keyPair();
} catch (e) {
console.error(`[hub] ${e.message}`);
process.exit(1);
}
// SMTP moved from .env to the Settings page in 1.2.0; carry the old values over once.
importSmtpFromEnv()
.then(done => { if (done) console.log('[hub] Copied SMTP_* from .env to Settings. You can remove them from .env now.'); })
.catch(e => console.warn('[hub] Could not copy SMTP_* from .env:', e?.message || e));
const PORT = parseInt(process.env.PORT || '5200', 10);
app.listen(PORT, () => console.log(`[hub] Listening on ${PORT}`));
// Background checks: alerts every 5 minutes, releases from git and billing every 15, heartbeat roll-up once a day. The Hub runs as a
// single process, so plain timers are enough.
if (String(process.env.JOBS_ENABLED || 'true').toLowerCase() !== 'false') {
const every = (ms, name, fn) => setInterval(() => fn().catch(e => console.warn(`[hub] ${name} failed:`, e?.message || e)), ms);
every(5 * 60 * 1000, 'alerts', () => runAlerts());
every(15 * 60 * 1000, 'release sync', () => syncReleases());
every(24 * 60 * 60 * 1000, 'heartbeat roll-up', () => rollupHeartbeats());
every(15 * 60 * 1000, 'billing', () => runBilling());
// Servers (2.1.0): the job scheduler (missed jobs, reminders, exports), server alerts, stats roll-up.
every(30 * 1000, 'jobs', () => jobs.tick());
every(60 * 1000, 'server alerts', () => servers.runServerAlerts());
every(24 * 60 * 60 * 1000, 'stats roll-up', () => servers.rollupStats());
setTimeout(() => runBilling().catch(e => console.warn('[hub] billing failed:', e?.message || e)), 60 * 1000);
setTimeout(() => runAlerts().catch(() => {}), 30 * 1000);
setTimeout(() => syncReleases().catch(e => console.warn('[hub] release sync failed:', e?.message || e)), 20 * 1000);
}