'use strict'; const config = require('./config'); const { createApp } = require('./app'); const db = require('./infra/db'); const redis = require('./infra/redis'); const modules = require('./modules/modules'); const precompute = require('./precompute'); async function bootstrap() { // 1. 注册预计算器 precompute.registerAll(); await require('./precompute/crew-metrics').ensureSchema(); // 2. 启动一致性校验(注册表 SSOT + modules 表对齐,§5.4) await modules.syncModulesOnBoot(); // 3. 起 API 服务;导出 Worker 以独立进程运行,避免阻塞 HTTP 请求。 const app = createApp(); const server = app.listen(config.port, '127.0.0.1', () => { console.log(`[server] listening on http://127.0.0.1:${config.port}/api/v1`); }); // 4. 优雅退出 const shutdown = async (sig) => { console.log(`[server] ${sig} received, shutting down...`); server.close(); await db.close().catch(() => {}); await redis.close().catch(() => {}); process.exit(0); }; process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGTERM', () => shutdown('SIGTERM')); } bootstrap().catch((err) => { console.error('[server] bootstrap failed:', err); process.exit(1); });