"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 cors_1 = __importDefault(require("cors")); const helmet_1 = __importDefault(require("helmet")); const dotenv_1 = __importDefault(require("dotenv")); const express_rate_limit_1 = __importDefault(require("express-rate-limit")); const http_1 = require("http"); const ws_1 = require("ws"); const path_1 = __importDefault(require("path")); // Import routes const auth_1 = __importDefault(require("./routes/auth")); const tenants_1 = __importDefault(require("./routes/tenants")); const knowledge_base_1 = __importDefault(require("./routes/knowledge-base")); const chat_1 = __importDefault(require("./routes/chat")); const analytics_1 = __importDefault(require("./routes/analytics")); const widget_1 = __importDefault(require("./routes/widget")); // Import middleware const auth_2 = require("./middleware/auth"); const errorHandler_1 = require("./middleware/errorHandler"); // Import database initialization const database_1 = require("./db/database"); // Import WebSocket handler const websocket_1 = require("./services/websocket"); dotenv_1.default.config(); const app = (0, express_1.default)(); const PORT = process.env.PORT || 3001; // Create HTTP server for WebSocket support const server = (0, http_1.createServer)(app); // Initialize WebSocket const wss = new ws_1.WebSocketServer({ server }); (0, websocket_1.setupWebSocket)(wss); // Security middleware app.use((0, helmet_1.default)({ contentSecurityPolicy: false, // Disable for development crossOriginEmbedderPolicy: false })); // Rate limiting const limiter = (0, express_rate_limit_1.default)({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100, // limit each IP to 100 requests per windowMs message: 'Too many requests from this IP, please try again later.' }); app.use('/api/', limiter); // CORS configuration app.use((0, cors_1.default)({ origin: [ process.env.FRONTEND_URL || 'http://localhost:5173', 'http://localhost:3000', 'https://your-frontend-domain.com' // Add your production frontend URL ], credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization'] })); // Body parsing middleware app.use(express_1.default.json({ limit: '50mb' })); app.use(express_1.default.urlencoded({ extended: true, limit: '50mb' })); // Static file serving for uploads app.use('/uploads', express_1.default.static(path_1.default.join(__dirname, '../uploads'))); // Health check endpoint app.get('/health', (req, res) => { res.json({ status: 'healthy', timestamp: new Date().toISOString(), version: '1.0.0' }); }); // API Routes app.use('/api/auth', auth_1.default); app.use('/api/tenants', auth_2.authenticateToken, tenants_1.default); app.use('/api/knowledge-base', auth_2.authenticateToken, knowledge_base_1.default); app.use('/api/chat', chat_1.default); // No auth required for public chat widget app.use('/api/analytics', auth_2.authenticateToken, analytics_1.default); app.use('/api/widget', widget_1.default); // No auth required for widget access // Error handling middleware app.use(errorHandler_1.errorHandler); // 404 handler app.use('*', (req, res) => { res.status(404).json({ error: 'Endpoint not found' }); }); // Initialize database and start server async function startServer() { try { await (0, database_1.initializeDatabase)(); console.log('✅ Database initialized successfully'); server.listen(PORT, () => { console.log(`🚀 Server running on port ${PORT}`); console.log(`📱 Health check: http://localhost:${PORT}/health`); console.log(`🔌 WebSocket server ready`); console.log(`🌐 CORS enabled for: ${process.env.FRONTEND_URL}`); }); } catch (error) { console.error('❌ Failed to start server:', error); process.exit(1); } } startServer(); exports.default = app; //# sourceMappingURL=server.js.map