Skip to content

Instantly share code, notes, and snippets.

@phmLabs
Last active June 23, 2016 17:48
Show Gist options
  • Save phmLabs/fdab2fd517e342c54163bbe2f7cb8e68 to your computer and use it in GitHub Desktop.
Save phmLabs/fdab2fd517e342c54163bbe2f7cb8e68 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var redis = require('redis');
var client = redis.createClient();
client.on('connect', function(){
console.log(' Redis connected.\n');
});
app.use (function(req, res, next) {
var data='';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
app.get('/health_check.html', function(req, res) {
var id = Math.floor(Math.random() * 1000000);
client.set('health_check', id);
client.get('health_check', function(err, reply) {
if(id != reply) {
res.status(500).send('Varnish not responding correct!');
// res.send('id: ' + id + ' - currentId: ' + reply);
}else{
res.send('OK');
}
});
});
app.post('/', function (req, res) {
var webhook = new Object();
webhook['payload'] = req.body;
webhook['url'] = req.url;
webhook['id'] = new Date().getTime() + '-' + Math.floor((Math.random() * 1000000) + 1);;
client.lpush('webhook_request', JSON.stringify(webhook), function(error) {
if(error) {
console.log(error);
}
});
res.send({'status': 'success', 'message': 'successfuly added to koalamon queue', 'id': webhook['id']});
console.log( ' - ' + webhook['id'] + ": success");
});
app.listen(3000, function () {
console.log('\n Webhook listener started.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment