UNPKG

@codai/cbd

Version:

Codai Better Database - High-Performance Vector Memory System with HPKV-inspired architecture and MCP server

111 lines โ€ข 4.82 kB
#!/usr/bin/env tsx /** * CBD Universal Database - Modern Express.js Startup * Based on Microsoft Azure best practices and Express.js 5.x patterns * * Features: * - Proper graceful shutdown handling * - Process lifecycle management * - Production-ready error handling * - Modern TypeScript with Express.js 5.x compatibility */ import { CBDUniversalServiceSimple } from './CBDUniversalService.js'; class ModernCBDServer { service = null; server = null; isShuttingDown = false; async start() { try { console.log('๐Ÿš€ Starting CBD Universal Database Service...'); console.log('๐Ÿ“‹ Environment:', process.env.NODE_ENV || 'development'); console.log('๐Ÿ”ง Node.js version:', process.version); // Initialize service this.service = new CBDUniversalServiceSimple(); const app = await this.service.initialize(); // Configure server const port = process.env.PORT || 4180; this.server = app.listen(port, () => { console.log('โœ… CBD Universal Database Service is running'); console.log(`๐ŸŒ Server: http://localhost:${port}`); console.log('๐Ÿ“Š Available Paradigms: 6 (Document, Vector, Graph, Key-Value, Time-Series, File Storage)'); console.log(''); console.log('๐Ÿ“ Key Endpoints:'); console.log(` Health Check: http://localhost:${port}/health`); console.log(` Statistics: http://localhost:${port}/stats`); console.log(` Document API: http://localhost:${port}/document/*`); console.log(` Vector API: http://localhost:${port}/vector/*`); console.log(` Graph API: http://localhost:${port}/graph/*`); console.log(` KV API: http://localhost:${port}/kv/*`); console.log(` TimeSeries: http://localhost:${port}/timeseries/*`); console.log(` Files API: http://localhost:${port}/files/*`); console.log(''); console.log('๐Ÿ’ก Ready to handle requests. Press Ctrl+C to stop.'); }); // Set server timeout for long-running operations this.server.timeout = 30000; // Setup graceful shutdown this.setupGracefulShutdown(); } catch (error) { console.error('โŒ Failed to start CBD service:', error); process.exit(1); } } setupGracefulShutdown() { // Handle termination signals properly const shutdown = async (signal) => { if (this.isShuttingDown) { console.log('๐Ÿ”„ Shutdown already in progress...'); return; } this.isShuttingDown = true; console.log(`\n๐Ÿ“ก Received ${signal}. Starting graceful shutdown...`); try { // Stop accepting new connections if (this.server) { console.log('๐Ÿ”’ Closing HTTP server...'); await new Promise((resolve, reject) => { this.server.close((err) => { if (err) reject(err); else resolve(); }); }); } // Cleanup service resources if (this.service) { console.log('๐Ÿงน Cleaning up service resources...'); // Note: Add service cleanup method if available // await this.service.cleanup(); } console.log('โœ… Graceful shutdown completed'); process.exit(0); } catch (error) { console.error('โŒ Error during shutdown:', error); process.exit(1); } }; // Register signal handlers process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGTERM', () => shutdown('SIGTERM')); // Handle uncaught exceptions process.on('uncaughtException', (error) => { console.error('๐Ÿ’ฅ Uncaught Exception:', error); shutdown('UNCAUGHT_EXCEPTION'); }); // Handle unhandled promise rejections process.on('unhandledRejection', (reason, promise) => { console.error('๐Ÿšจ Unhandled Rejection at:', promise, 'reason:', reason); shutdown('UNHANDLED_REJECTION'); }); } } // Start the server const server = new ModernCBDServer(); server.start().catch((error) => { console.error('๐Ÿ’ฅ Fatal startup error:', error); process.exit(1); }); //# sourceMappingURL=start.js.map