"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = __importDefault(require("express")); const config_1 = require("./utils/config"); const logger_1 = require("./utils/logger"); const websocket_1 = require("./http/websocket"); const auth_1 = require("./http/auth"); class DebugProxyServer { app; server = null; logger; isShuttingDown = false; wsHandler; authValidator; constructor() { this.logger = new logger_1.Logger('DebugProxyServer'); this.app = (0, express_1.default)(); this.authValidator = new auth_1.AuthValidator(); this.wsHandler = new websocket_1.WebSocketHandler(this.authValidator); this.setupMiddleware(); this.setupRoutes(); this.setupShutdownHandlers(); } setupMiddleware() { // Parse JSON bodies this.app.use(express_1.default.json()); // Request logging this.app.use((req, _res, next) => { this.logger.info(`${req.method} ${req.path}`); next(); }); } setupRoutes() { // Public endpoints (no auth) this.app.get('/_ide/debug/status', (_req, res) => { res.json({ status: 'healthy', service: 'rspade-debug-proxy', version: '1.0.0', uptime: process.uptime(), memory: process.memoryUsage() }); }); // Ping endpoint for basic connectivity test this.app.get('/_ide/debug/ping', (_req, res) => { res.json({ pong: true }); }); // Authenticated endpoints this.app.use('/_ide/debug/api', this.authValidator.authMiddleware()); this.app.post('/_ide/debug/api/test', (req, res) => { const sessionId = req.sessionId; res.json({ success: true, message: 'Authenticated successfully', sessionId: sessionId }); }); // 404 handler this.app.use((req, res) => { res.status(404).json({ error: 'Not Found', path: req.path }); }); // Error handler this.app.use((err, _req, res, _next) => { this.logger.error('Request error:', err); res.status(500).json({ error: 'Internal Server Error', message: err.message }); }); } setupShutdownHandlers() { const shutdown = async (signal) => { if (this.isShuttingDown) { return; } this.isShuttingDown = true; this.logger.info(`Received ${signal}, starting graceful shutdown...`); // Close HTTP server if (this.server) { this.server.close(() => { this.logger.info('HTTP server closed'); }); } // Give connections time to close setTimeout(() => { this.logger.info('Shutdown complete'); process.exit(0); }, 5000); }; process.on('SIGTERM', () => shutdown('SIGTERM')); process.on('SIGINT', () => shutdown('SIGINT')); } async start() { const port = config_1.Config.HTTP_PORT; return new Promise((resolve, reject) => { this.server = this.app.listen(port, () => { this.logger.info(`Debug proxy service started on port ${port}`); this.logger.info(`Status endpoint: http://localhost:${port}/_ide/debug/status`); this.logger.info(`WebSocket endpoint: ws://localhost:${port}/_ide/debug/ws`); // Initialize WebSocket handler this.wsHandler.initialize(this.server); this.logger.info('WebSocket handler initialized'); resolve(); }); this.server.on('error', (error) => { this.logger.error('Failed to start server:', error); reject(error); }); }); } } // Main entry point async function main() { const server = new DebugProxyServer(); try { await server.start(); } catch (error) { console.error('Failed to start debug proxy:', error); process.exit(1); } } // Start the server main().catch(error => { console.error('Uncaught error:', error); process.exit(1); }); //# sourceMappingURL=server.js.map