aethercall
Version:
A scalable WebRTC video calling API built with Node.js and OpenVidu
93 lines (77 loc) • 3.19 kB
JavaScript
/**
* AetherCall API Main Entry Point
* Initializes and starts the HTTP server with all dependencies
*/
const HTTPServer = require('./src/interfaces/http/server');
const OpenViduAPI = require('./src/core/openvidu/api');
const StorageAdapter = require('./src/core/storage/adapter');
const TokenManager = require('./src/core/auth/tokens');
const config = require('./src/utils/config');
const logger = require('./src/utils/logger');
async function startServer() {
try {
logger.info('Starting AetherCall API Server...');
// Initialize storage
logger.info('Initializing storage adapter...', { type: config.dbType });
const storage = new StorageAdapter(config.getDatabaseConfig());
await storage.initialize();
// Initialize token manager
logger.info('Initializing token manager...');
const tokenManager = new TokenManager(config.jwtSecret, config.jwtExpiresIn);
// Initialize OpenVidu API
logger.info('Initializing OpenVidu API...', {
url: config.openviduUrl,
// Don't log the secret for security
hasSecret: !!config.openviduSecret
});
const openviduAPI = new OpenViduAPI(config.openviduUrl, config.openviduSecret);
// Create HTTP server with dependencies
logger.info('Setting up HTTP server...');
const httpServer = new HTTPServer({
openviduAPI,
storage,
tokenManager
});
// Start the server
await httpServer.start(config.port, config.host);
logger.info('AetherCall API Server started successfully!', {
port: config.port,
host: config.host,
environment: config.nodeEnv,
endpoints: {
health: `http://${config.host}:${config.port}/health`,
api: `http://${config.host}:${config.port}/api`,
docs: `http://${config.host}:${config.port}/docs`
}
});
// Graceful shutdown handling
const gracefulShutdown = async (signal) => {
logger.info(`Received ${signal}. Starting graceful shutdown...`);
try {
await httpServer.stop();
await storage.close();
logger.info('Graceful shutdown completed');
process.exit(0);
} catch (error) {
logger.error('Error during shutdown:', error);
process.exit(1);
}
};
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
// Log system metrics periodically in development
if (config.isDevelopment()) {
setInterval(() => {
logger.logMetrics();
}, 60000); // Every minute
}
} catch (error) {
logger.error('Failed to start AetherCall API Server:', error);
process.exit(1);
}
}
// Start the server
if (require.main === module) {
startServer();
}
module.exports = { startServer };