Skip to content

Instantly share code, notes, and snippets.

@ryanhugh
Created September 22, 2019 16:57
Show Gist options
  • Save ryanhugh/961cc8fb505ccc640505e8a23ecdcf5b to your computer and use it in GitHub Desktop.
Save ryanhugh/961cc8fb505ccc640505e8a23ecdcf5b to your computer and use it in GitHub Desktop.
// import request from 'request';
import express from 'express';
import macros from './backend/macros';
import bodyParser from 'body-parser';
// macros.PROD=true;
// macros.DEV=false;
// import database from './backend/database';
// import domutils from 'domutils';
// import server from './backend/server';
// const app = express();
// const express = require('express')
const app = express()
const port = 3000
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// Process application/json
app.use(bodyParser.json());
app.get('/', (req, res) => res.send('Hello World!'))
// TODO: clean up old data after a few seconds pass
let globalMap = {
}
app.get('/browserRequest', (req, res) => {
if (!req.query.userid) {
console.log('invalid id')
return;
}
console.log("browserRequest with id ", req.query.userid)
// If the data already exists in the map, use it.
if (globalMap[req.query.userid] && globalMap[req.query.userid].webhookdata) {
console.log("found request in map")
res.send(globalMap[req.query.userid].webhookdata)
// And clear out the data
globalMap[req.query.userid] = null;
return;
}
// If not, put this data in the webhook to use save for a few seconds
else {
console.log("found NOT request in map, putting req and res in there")
globalMap[req.query.userid] = {req:req, res:res}
// Don't call res.send
}
})
app.get('/webhook', (req, res) => {
if (!req.query.webhookdata || !req.query.userid) {
console.log("invalid webhook")
// Always respond with a 200 to the webhook.
res.send("Invalid webhook")
return;
}
console.log("got a webhook with data", req.query.webhookdata, 'and user id:', req.query.userid)
// If the data already exists in the map, use it.
if (globalMap[req.query.userid] && globalMap[req.query.userid].res) {
// respond to the first request
globalMap[req.query.userid].res.send(req.query.webhookdata)
// And clear out the data
globalMap[req.query.userid] = null;
console.log("in webhook, found the request data there ,calling res ")
}
// If not, put this data in the webhook to use save for a few seconds
else {
console.log("putting the webhookdata in the map")
globalMap[req.query.userid] = {webhookdata:req.query.webhookdata}
}
// Allways respond with a 200 to the webhook.
res.sendStatus(200)
})
setInterval(() => {
console.log("Current data: ")
for (let key of Object.keys(globalMap)) {
console.log(key, globalMap[key])
}
// console.log(JSON.stringify(globalMap, null, 4))
}, 3000)
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment