const crypto = require('crypto'); // Đảm bảo crypto có sẵn toàn cục cho các thư viện cũ hoặc plugin mongoose global.crypto = crypto; const express = require('express'); const cors = require('cors'); const path = require('path'); const dotenv = require('dotenv'); const connectDB = require('./config/db'); // Cấu hình môi trường dotenv.config(); // Kiểm tra các biến môi trường bắt buộc const requiredEnvs = ['MONGODB_URI', 'JWT_SECRET']; requiredEnvs.forEach(env => { if (!process.env[env]) { console.error(`[CRITICAL] Thiếu biến môi trường bắt buộc: ${env}`); process.exit(1); } }); // Kết nối cơ sở dữ liệu MongoDB connectDB(); const app = express(); // Middlewares cơ bản app.use(cors()); app.use(express.json()); // Khởi tạo Worker xử lý ảnh (BullMQ + Redis) // Việc import này sẽ kích hoạt imageWorker.js lắng nghe hàng đợi 'image-processing' require('./routes/imageWorker'); // Đăng ký các API Routes tập trung app.use('/api', require('./routes/apiRoutes')); // Phục vụ các tệp tĩnh từ thư mục frontend app.use(express.static(path.join(__dirname, '../frontend'))); // Hỗ trợ Single Page Application (SPA) // Mọi request không khớp với API hoặc File tĩnh sẽ trả về index.html app.get(/.*/, (req, res) => { res.sendFile(path.join(__dirname, '../frontend/index.html')); }); const PORT = process.env.PORT || 5000; app.listen(PORT, () => { console.log(`================================================`); console.log(`🚀 Server 3D Tours đang chạy tại port: ${PORT}`); console.log(`🔧 Chế độ: ${process.env.NODE_ENV || 'development'}`); console.log(`================================================`); });