Spaces:
Sleeping
Sleeping
Download src/index.js from Discord-Core-Bot/gateway-services: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Discord-Core-Bot/gateway-services/resolve/main/src/index.js
- Command line
-
hf download hf://spaces/Discord-Core-Bot/gateway-services/src/index.js
-
curl -L -o index.js https://huggingface.co/spaces/Discord-Core-Bot/gateway-services/resolve/main/src/index.js
1.71 kB
| 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()}`); | |
| }); | |