import express from 'express'; import router from './router.js'; const REQUIRED_ENV = [ 'DISCORD_PUBLIC_KEY', 'DISCORD_CLIENT_ID', 'HF_TOKEN', 'HF_BUCKET_NAME', 'API_SECRET', ]; console.log('[startup] 🚀 Validating environment variables...'); for (const key of REQUIRED_ENV) { if (!process.env[key]) { console.error(`[startup] ❌ Missing required environment variable: ${key}`); process.exit(1); } } console.log('[startup] Environment variables checked successfully.'); const app = express(); // CRITICAL HF FIX: Trust proxy settings to retain original HTTPS and IP headers app.set('trust proxy', 1); // Middleware to log incoming network connection basic info app.use((req, res, next) => { console.log(`\n--- [INCOMING REQUEST] --- [${new Date().toISOString()}]`); console.log(`Method: ${req.method} | Path: ${req.path}`); console.log(`Real Client IP: ${req.ip}`); console.log(`Is Secure (HTTPS): ${req.secure}`); next(); }); // Capture raw body buffer for verification and log its buffer length app.use(express.json({ limit: '10mb', verify: (req, res, buf) => { req.rawBody = buf; if (buf) { console.log(`[body-parser] Captured raw body buffer. Length: ${buf.length} bytes`); } } })); // Mount all routes app.use('/', router); // Global Error Handler for parsing errors app.use((err, req, res, next) => { console.error('[server-error] Global error intercepted:', err.message); res.status(500).json({ error: 'Internal Server Error', message: err.message }); }); const PORT = process.env.PORT || 7860; app.listen(PORT, () => { console.log(`[startup] ✅ gateway-services running on port ${PORT} — ${new Date().toISOString()}`); });