/** * Starts the Church Website's backend: database, HTTP server, and (on a Hub install) registering * with the Hub from HUB_LICENCE_KEY, then heartbeats, which also link the site to the church's * Accounts once the Hub lists it, and the hourly YouTube feed (§7). The other jobs (storage emails, * the event feed) come with their features (docs/WEBSITE-1.0-PLAN.md §1). */ 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, startHeartbeats } from './hub/client.js'; import { loadConfig } from './lib/config.js'; import { startYoutubeSync } from './site/youtube.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); const app = createApp(ctx); const server = app.listen(config.port, () => console.log(`[website] Listening on ${config.port} (${config.publicUrl}).`)); const stopHeartbeats = startHeartbeats(ctx); const stopYoutube = startYoutubeSync(ctx); connectFromEnv(ctx).then((r) => { if (r.connected || r.reason === 'already') heartbeat(ctx).catch(() => {}); }).catch(() => {}); const stop = async () => { stopHeartbeats(); stopYoutube(); 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); });