@codai/cbd
Version:
Codai Better Database - High-Performance Vector Memory System with HPKV-inspired architecture and MCP server
111 lines โข 4.82 kB
JavaScript
/**
* 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