const crypto = require('crypto');
const http = require('http');
const PORT = process.env.PORT || 3000; // Use environment variable for port or default to 3000
const GLYDE_SIGNING_KEY = 'YOUR_SIGNING_KEY'; // Replace with your actual signing key
const server = http.createServer((req, res) => {
if (req.method !== 'POST' || !req.headers['http-x-signature-hash']) {
res.statusCode = 400;
res.end('Invalid request method or missing signature header.');
return;
}
let inputData = '';
req.on('data', (chunk) => {
inputData += chunk.toString();
});
req.on('end', () => {
const signature = req.headers['http-x-signature-hash'];
const calculatedHash = crypto.createHmac('sha256', GLYDE_SIGNING_KEY)
.update(inputData)
.digest('hex');
if (signature !== calculatedHash) {
res.statusCode = 401;
res.end('Invalid signature.');
return;
}
res.statusCode = 200;
res.end();
// Parse JSON data (assuming it's in the request body)
try {
const event = JSON.parse(inputData);
// Process the data in the 'event' object here
console.log('Received event:', event);
} catch (error) {
console.error('Error parsing JSON:', error.message);
// Handle JSON parsing error appropriately (e.g., send error response)
}
});
});
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});