Skip to content
Snippets Groups Projects
Commit 6dd72a39 authored by Markos Calderon's avatar Markos Calderon
Browse files

Finish webhook API callback and remove unneeded demo app

parent d730300c
No related branches found
No related tags found
No related merge requests found
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var redis = require('redis');
var db = redis.createClient();
var app = express();
express.request.db = express.response.db = db;
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.post('/update', routes.update);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.0rc4",
"jade": "*",
"redis": "*"
}
}
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
\ No newline at end of file
/*
* GET home page.
*/
exports.index = function(req, res){
req.db.get("titleTest",function(err,result){
res.render('index', { title: result });
});
};
exports.update = function(req, res){
console.log("executing update");
req.db.set("titleTest",req.body.titleTest, function(err,result){
console.log("done!");
res.writeHead(200);
res.end();
});
};
/*
* GET users listing.
*/
exports.list = function(req, res){
res.send("respond with a resource");
};
\ No newline at end of file
extends layout
block content
h1= title
p Welcome to #{title}
\ No newline at end of file
doctype 5
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
\ No newline at end of file
var querystring = require('querystring'),
http = require('http'),
url = require('url'),
var request = require('request'),
redis = require("redis"),
subscriber = redis.createClient(),
client = redis.createClient();
......@@ -10,50 +8,37 @@ subscriber.on("subscribe", function (channel, count) {
});
subscriber.on("message", function (channel, message) {
console.log(message);
properties = JSON.parse(message);
console.log(properties.event)
console.log(properties.meetingID)
client.lrange("meeting:" + properties.meetingID + ":subscriptions:" + properties.event, 0, -1, function(error,reply){
reply.forEach(function (sid, index) {
console.log(sid);
client.hgetall("meeting:" + properties.meetingID + ":subscription:" + sid, function(err,rep){
console.log(rep);
var post_data = querystring.stringify(properties);
var url_parts = url.parse(rep.callbackURL, true);
var post_options = {
host: url_parts.hostname,
port: (url_parts.port == undefined) ? '80' : url_parts.port,
path: url_parts.path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
var properties;
try {
properties = JSON.parse(message);
} catch (e) {
// An error has occured, handle it, by e.g. logging it
console.log(e);
}
if(properties != undefined){
client.lrange("meeting:" + properties.meetingID + ":subscriptions:" + properties.event, 0, -1, function(error,reply){
reply.forEach(function (sid, index) {
console.log(sid);
client.hgetall("meeting:" + properties.meetingID + ":subscription:" + sid, function(err,rep){
if(rep.active == "true"){
var post_options = {
uri: rep.callbackURL,
method: 'POST',
json: properties
};
request(post_options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body.id) // Print the shortened url.
}
});
}
};
console.log(post_options);
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.on('error',function(badreq){
console.log(badreq);
});
// write parameters to post body
post_req.write(post_data);
post_req.end();
});
});
});
}
});
......
......@@ -8,7 +8,8 @@
"callbacks"
],
"dependencies" : {
"redis": "0.8.3"
"redis": "0.8.3",
"request": "*"
},
"engines": {
"node": "0.8.21"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment