import { createApp } from './app'; import { config } from './config'; import { schedulerService } from './services/schedulerService'; import { prisma } from './utils/prisma'; async function main() { try { console.log('Starting WLED Controller Backend...'); // Initialize scheduler await schedulerService.initialize(); // Create Express app const app = createApp(); // Start server const server = app.listen(config.port, () => { console.log(`Server running on port ${config.port}`); console.log(`Health check: http://localhost:${config.port}/health`); }); // Graceful shutdown const shutdown = async () => { console.log('\nShutting down gracefully...'); // Stop scheduler await schedulerService.shutdown(); // Close database connection await prisma.$disconnect(); // Close server server.close(() => { console.log('Server closed'); process.exit(0); }); // Force exit after 10 seconds setTimeout(() => { console.error('Forced shutdown after timeout'); process.exit(1); }, 10000); }; process.on('SIGTERM', shutdown); process.on('SIGINT', shutdown); } catch (error) { console.error('Failed to start server:', error); await prisma.$disconnect(); process.exit(1); } } main();