/** * Starts the Church Website's backend: database, HTTP server, and (on a Hub install) registering * with the Hub from HUB_LICENCE_KEY, then the background jobs (src/jobs.ts, 1.4.0): heartbeats, * which also link the site to the church's Accounts once the Hub lists it, the event feed, the * hourly YouTube feed (§7) and the nightly database backup. */ import 'dotenv/config'; import { PrismaPg } from '@prisma/adapter-pg'; import { PrismaClient } from './generated/prisma/client.js'; import { createApp } from './app.js'; import { createCtx } from './context.js'; import { connectFromEnv, heartbeat } from './hub/client.js'; import { startJobs } from './jobs.js'; import { loadConfig } from './lib/config.js'; import { ensureStarterPages } from './site/starter.js'; async function main() { const config = loadConfig(); const url = process.env.DATABASE_URL; if (!url) throw new Error('DATABASE_URL isn\'t set.'); const db = new PrismaClient({ adapter: new PrismaPg({ connectionString: url, connectionTimeoutMillis: 5000 }) }); const ctx = createCtx(config, db); // A new install's fixed pages (home, Plan your visit, …), before anything is served. await ensureStarterPages(ctx) .then((kinds) => { if (kinds.length) console.log(`[website] Added the starter pages: ${kinds.join(', ')}.`); }) .catch((e) => console.error('[website] Couldn\'t add the starter pages:', (e as Error).message)); const app = createApp(ctx); const server = app.listen(config.port, () => console.log(`[website] Listening on ${config.port} (${config.publicUrl}).`)); // 1.4.0: the heartbeat, the event feed, YouTube and the nightly backup, on pg-boss. const stopJobs = await startJobs(ctx, url); connectFromEnv(ctx).then((r) => { if (r.connected || r.reason === 'already') heartbeat(ctx).catch(() => {}); }).catch(() => {}); const stop = async () => { await stopJobs(); server.close(); await db.$disconnect(); process.exit(0); }; process.on('SIGTERM', stop); process.on('SIGINT', stop); } main().catch((e) => { console.error('[website] Failed to start:', e); process.exit(1); });