Wifiv2 / src /server.js
Mbonea's picture
fix(logging): surface migration/SQL errors so boot crashes are diagnosable
f63dc3d
Raw
History Blame Contribute Delete
1.56 kB
require('dotenv').config();
require('./utils/logger').install();
const app = require('./app');
const omada = require('./services/omada');
const { initJobs } = require('./jobs');
const { runMigrations } = require('./utils/migrate');
const PORT = process.env.PORT || 3000;
async function start() {
// 1. Run DB migrations
try {
await runMigrations();
} catch (err) {
console.error('[Boot] Migration failed — aborting:', err.message);
if (err.stack) console.error(err.stack);
if (err.errno || err.code || err.sqlState) {
console.error('[Boot] DB error:', JSON.stringify({ errno: err.errno, code: err.code, sqlState: err.sqlState, sqlMessage: err.sqlMessage }));
}
if (err.migrationContext) {
console.error('[Boot] Migration context:', JSON.stringify(err.migrationContext));
}
process.exit(1);
}
// 2. Boot Omada connection
try {
await omada.initOmada();
} catch (err) {
console.error('[Boot] Omada init failed:', err.message);
console.warn('[Boot] Continuing without Omada — check OMADA_URL / credentials');
}
// 3. Start background jobs
initJobs();
// 3. Listen
app.listen(PORT, () => {
console.log(`[Server] Listening on http://0.0.0.0:${PORT}`);
const host = (process.env.PORTAL_HOST || '').replace(/^https?:\/\//, '');
console.log(`[Server] Portal base: ${process.env.PORTAL_SCHEME || 'http'}://${host}`);
});
}
start().catch(err => {
console.error('[Boot] Fatal error:', err.message);
if (err.stack) console.error(err.stack);
process.exit(1);
});