import 'dotenv/config'
import { initDb, closeDb } from './db.js'
import { createApp } from './app.js'
import { bootstrapSuperAdmin } from './routes/auth.js'
import { startScheduler } from './scheduler.js'
const PORT = Number(process.env.PORT || 3001)
async function main() {
await initDb()
await bootstrapSuperAdmin()
const app = createApp()
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`[server] 云客 CRM Pro API 已启动: http://0.0.0.0:${PORT}`)
})
startScheduler()
const shutdown = async () => {
server.close(async () => { await closeDb(); process.exit(0) })
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
}
main().catch(err => { console.error('[fatal] 启动失败:', err); process.exit(1) })