File size: 1,709 Bytes
f0fd97c
 
e903adb
 
 
 
 
 
 
f0fd97c
bae62ce
a7781c6
e903adb
 
a7781c6
f0fd97c
e903adb
 
a7781c6
e903adb
f0fd97c
e903adb
a7781c6
541bc0a
 
a7781c6
 
 
 
 
 
 
 
 
 
d20acf4
 
 
 
a7781c6
 
 
b39e375
d20acf4
e903adb
d20acf4
f0fd97c
e903adb
a7781c6
 
 
 
 
 
f0fd97c
e903adb
a7781c6
d20acf4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()}`);
});