startup.jsā¢1.92 kB
#!/usr/bin/env node
/**
* Startup Script
* Downloads database and starts MCP server
*/
import { downloadDatabase, checkDatabaseHealth } from './download-database.js';
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// Get __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function startup() {
console.log('š Starting EGW Research Server...');
// Download database if needed
const downloadSuccess = await downloadDatabase();
if (!downloadSuccess) {
console.error('ā Failed to download database, server cannot start');
process.exit(1);
}
// Verify database health
const dbHealthy = checkDatabaseHealth();
if (!dbHealthy) {
console.error('ā Database health check failed, server cannot start');
process.exit(1);
}
console.log('ā
Database ready, starting MCP server...');
// Start MCP server
const serverPath = path.join(__dirname, 'server-universal.js');
const serverProcess = spawn('node', [serverPath], {
stdio: 'inherit',
env: { ...process.env, NODE_ENV: 'production' }
});
serverProcess.on('error', (error) => {
console.error('ā Failed to start server:', error);
process.exit(1);
});
serverProcess.on('exit', (code) => {
console.log(`š Server exited with code ${code}`);
process.exit(code);
});
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('š Received SIGTERM, shutting down gracefully...');
serverProcess.kill('SIGTERM');
});
process.on('SIGINT', () => {
console.log('š Received SIGINT, shutting down gracefully...');
serverProcess.kill('SIGINT');
});
}
// Start application
startup().catch(error => {
console.error('ā Startup failed:', error);
process.exit(1);
});