Spaces:
Sleeping
Sleeping
Update src/index.js
Browse files- src/index.js +14 -13
src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
import express from 'express'
|
| 2 |
-
import router from './router.js'
|
| 3 |
import bodyParser from 'body-parser';
|
|
|
|
| 4 |
|
| 5 |
const REQUIRED_ENV = [
|
| 6 |
'DISCORD_PUBLIC_KEY',
|
|
@@ -8,38 +8,39 @@ const REQUIRED_ENV = [
|
|
| 8 |
'HF_TOKEN',
|
| 9 |
'HF_BUCKET_NAME',
|
| 10 |
'API_SECRET',
|
| 11 |
-
]
|
| 12 |
|
| 13 |
for (const key of REQUIRED_ENV) {
|
| 14 |
if (!process.env[key]) {
|
| 15 |
-
console.error(`[startup] Missing required environment variable: ${key}`)
|
| 16 |
-
process.exit(1)
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
-
const app = express()
|
| 21 |
|
| 22 |
-
// ====================
|
| 23 |
app.use((req, res, next) => {
|
| 24 |
if (req.path === '/interactions') {
|
|
|
|
| 25 |
bodyParser.raw({
|
| 26 |
type: 'application/json',
|
| 27 |
limit: '10mb'
|
| 28 |
})(req, res, (err) => {
|
| 29 |
if (err) return next(err);
|
| 30 |
|
| 31 |
-
req.rawBody = req.body;
|
| 32 |
next();
|
| 33 |
});
|
| 34 |
} else {
|
|
|
|
| 35 |
bodyParser.json({ limit: '10mb' })(req, res, next);
|
| 36 |
}
|
| 37 |
});
|
| 38 |
|
| 39 |
-
/
|
| 40 |
-
app.use('/', router)
|
| 41 |
|
| 42 |
-
const PORT = process.env.PORT || 7860
|
| 43 |
app.listen(PORT, () => {
|
| 44 |
-
console.log(`[startup] gateway-services running on port ${PORT} — ${new Date().toISOString()}`)
|
| 45 |
-
})
|
|
|
|
| 1 |
+
import express from 'express';
|
|
|
|
| 2 |
import bodyParser from 'body-parser';
|
| 3 |
+
import router from './router.js';
|
| 4 |
|
| 5 |
const REQUIRED_ENV = [
|
| 6 |
'DISCORD_PUBLIC_KEY',
|
|
|
|
| 8 |
'HF_TOKEN',
|
| 9 |
'HF_BUCKET_NAME',
|
| 10 |
'API_SECRET',
|
| 11 |
+
];
|
| 12 |
|
| 13 |
for (const key of REQUIRED_ENV) {
|
| 14 |
if (!process.env[key]) {
|
| 15 |
+
console.error(`[startup] Missing required environment variable: ${key}`);
|
| 16 |
+
process.exit(1);
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
+
const app = express();
|
| 21 |
|
| 22 |
+
// ==================== FIXED MIDDLEWARE ====================
|
| 23 |
app.use((req, res, next) => {
|
| 24 |
if (req.path === '/interactions') {
|
| 25 |
+
// For Discord interactions: must keep raw body as Buffer
|
| 26 |
bodyParser.raw({
|
| 27 |
type: 'application/json',
|
| 28 |
limit: '10mb'
|
| 29 |
})(req, res, (err) => {
|
| 30 |
if (err) return next(err);
|
| 31 |
|
| 32 |
+
req.rawBody = req.body; // This is now a Buffer (important!)
|
| 33 |
next();
|
| 34 |
});
|
| 35 |
} else {
|
| 36 |
+
// Normal JSON for other routes
|
| 37 |
bodyParser.json({ limit: '10mb' })(req, res, next);
|
| 38 |
}
|
| 39 |
});
|
| 40 |
|
| 41 |
+
app.use('/', router);
|
|
|
|
| 42 |
|
| 43 |
+
const PORT = process.env.PORT || 7860;
|
| 44 |
app.listen(PORT, () => {
|
| 45 |
+
console.log(`[startup] gateway-services running on port ${PORT} — ${new Date().toISOString()}`);
|
| 46 |
+
});
|