index.js (1562B)
1 require('./src/logger').install(); 2 3 const express = require('express'); 4 const { PORT } = require('./src/config'); 5 const { warmCache } = require('./src/cache'); 6 const { startWhatsApp } = require('./src/whatsapp'); 7 const { startHeartbeat } = require('./src/sseManager'); 8 9 const authRoutes = require('./src/routes/auth'); 10 const apiRoutes = require('./src/routes/api'); 11 const logsRoutes = require('./src/routes/logs'); 12 const statusRoutes = require('./src/routes/status'); 13 14 // Initialize Express 15 const app = express(); 16 app.use(express.urlencoded({ extended: true })); 17 app.use(express.json()); 18 // Static assets (favicon/logo) — served unauthenticated, before any login check 19 app.use(express.static('public')); 20 21 app.use((req, res, next) => { 22 res.header("Access-Control-Allow-Origin", "*"); 23 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); 24 next(); 25 }); 26 27 // Answer every OPTIONS preflight immediately — before ANY auth middleware sees it 28 app.options('*', (req, res) => { 29 res.sendStatus(200); 30 }); 31 32 startHeartbeat(); 33 34 // --- ROUTES --- 35 // Unauthenticated: /ping, SSE + action endpoints (own token-based auth), and system logs 36 app.use(apiRoutes); 37 app.use(logsRoutes); 38 // Login/logout/verify (unauthenticated by nature) 39 app.use(authRoutes); 40 // Cookie-authenticated web UI (home/status/QR page) — must be last, applies checkAuth 41 app.use(statusRoutes); 42 43 // --- START SERVER --- 44 app.listen(PORT, () => { 45 warmCache(); 46 startWhatsApp(); 47 console.log(`Server running on port ${PORT}`); 48 });