/** * Starts Accounts: database, OIDC provider, HTTP server, jobs, and (on a Hub install) registering * with the Hub from HUB_LICENCE_KEY. */ 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 { createProvider } from './oidc/provider.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 provider = await createProvider(ctx); const app = createApp(ctx, provider); const server = app.listen(config.port, () => console.log(`[accounts] Listening on ${config.port} (${config.publicUrl}).`)); const stopJobs = await startJobs(ctx, url); connectFromEnv(ctx).then((r) => { if (r.connected) heartbeat(ctx).catch(() => {}); }).catch(() => {}); const stop = async () => { server.close(); await stopJobs(); await db.$disconnect(); process.exit(0); }; process.on('SIGTERM', stop); process.on('SIGINT', stop); } main().catch((e) => { console.error('[accounts] Failed to start:', e); process.exit(1); });