Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import express from 'express'; import cors from 'cors'; import { config } from './config'; import { routes } from './routes'; import { SignerService } from './services/signer'; import { SessionManager } from './utils/session-manager'; import { EmailService } from './services/email'; import { authTokens } from './utils/auth-tokens'; import { errorHandler, requestIdMiddleware } from './middleware/error-handler'; import { Logger } from './utils/logger'; // Initialisation de l'application Express const app = express(); const PORT = config.port; // Request ID middleware (must be first) app.use(requestIdMiddleware); // Configuration CORS app.use(cors(config.cors)); app.use(express.json()); // Request logging middleware app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { const duration = Date.now() - start; Logger.logRequest(req, res, duration); }); next(); }); // Use routes from the reorganized structure app.use(routes); // Error handling middleware (must be last) app.use(errorHandler); // Initialize signer service with enhanced reconnection logic (async () => { try { const result = await SignerService.initialize(); if (result.success) { Logger.info('Signer service initialized'); } else { Logger.error('Failed to initialize signer service', { error: result.error?.message || 'Unknown error' }); } } catch (error) { Logger.error('Critical error during signer initialization', { error: error instanceof Error ? error.message : 'Unknown error' }); } })(); // Set up signer connection monitoring SignerService.onConnectionChange((connected) => { if (connected) { Logger.info('Signer connected'); } else { Logger.warn('Signer disconnected'); } }); // Cleanup expired sessions every 5 minutes setInterval(() => { SessionManager.cleanupExpiredSessions(); }, 5 * 60 * 1000); // Cleanup expired auth tokens every hour setInterval(() => { const now = Date.now(); const initialLength = authTokens.length; // Remove expired tokens for (let i = authTokens.length - 1; i >= 0; i--) { if (now > authTokens[i].expiresAt) { authTokens.splice(i, 1); } } const cleanedCount = initialLength - authTokens.length; if (cleanedCount > 0) { console.log(`Cleaned up ${cleanedCount} expired auth tokens`); } }, 60 * 60 * 1000); // Every hour // Automatic retry system for failed emails setInterval(() => { EmailService.retryFailedEmails(); }, 60000); // Check every minute // Initialisation et démarrage du serveur async function startServer(): Promise<void> { try { // Démarrage du serveur app.listen(PORT, () => { console.log(`Server started on port ${PORT}`); }); } catch (error) { console.error('Error starting the server:', error); process.exit(1); } } // Graceful shutdown handling process.on('SIGTERM', () => { Logger.info('SIGTERM received, shutting down gracefully'); SignerService.cleanup(); process.exit(0); }); process.on('SIGINT', () => { Logger.info('SIGINT received, shutting down gracefully'); SignerService.cleanup(); process.exit(0); }); // Handle uncaught exceptions process.on('uncaughtException', (error) => { Logger.error('Uncaught exception', { error: error.message, stack: error.stack }); SignerService.cleanup(); process.exit(1); }); // Handle unhandled promise rejections process.on('unhandledRejection', (reason, promise) => { Logger.error('Unhandled promise rejection', { reason: reason instanceof Error ? reason.message : String(reason), stack: reason instanceof Error ? reason.stack : undefined }); }); // Démarrage de l'application startServer(); |