Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Initial revision ported from CF example
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bridgen committed Aug 26, 2011
0 parents commit dbf951f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .hgignore
@@ -0,0 +1,2 @@
.git
node_modules
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: node app.js
13 changes: 13 additions & 0 deletions README
@@ -0,0 +1,13 @@
This is a simple Node.JS application demonstrating the use of the
RabbitMQ service on Cloud Foundry and Heroku.

To run locally, make sure RabbitMQ is running, then:

$ npm install
$ node app.js

or, in the Heroku way:

$ gem install foreman
$ npm install
$ foreman start
117 changes: 117 additions & 0 deletions app.js
@@ -0,0 +1,117 @@
require.paths.unshift('./node_modules');

var http = require('http');
var amqp = require('amqp');
var URL = require('url');
var htmlEscape = require('sanitizer/sanitizer').escape;

function rabbitUrl() {
if (process.env.VCAP_SERVICES) {
conf = JSON.parse(process.env.VCAP_SERVICES);
return conf['rabbitmq-2.4'][0].credentials.url;
}
else if (process.env.RABBITMQ_URL) {
return process.env.RABBITMQ_URL;
}
else {
return "amqp://localhost";
}
}

var port = process.env.VCAP_APP_PORT || process.env.PORT || 3000;

var messages = [];

function setup() {

var exchange = conn.exchange('cf-demo', {'type': 'fanout', durable: false}, function() {

var queue = conn.queue('', {durable: false, exclusive: true},
function() {
queue.subscribe(function(msg) {
messages.push(htmlEscape(msg.body));
if (messages.length > 10) {
messages.shift();
}
});
queue.bind(exchange.name, '');
});
queue.on('queueBindOk', function() { httpServer(exchange); });
});
}

function httpServer(exchange) {
var serv = http.createServer(function(req, res) {
var url = URL.parse(req.url);
if (req.method == 'GET' && url.pathname == '/env') {
printEnv(res);
}
else if (req.method == 'GET' && url.pathname == '/') {
res.statusCode = 200;
openHtml(res);
writeForm(res);
writeMessages(res);
closeHtml(res);
}
else if (req.method == 'POST' && url.pathname == '/') {
chunks = '';
req.on('data', function(chunk) { chunks += chunk; });
req.on('end', function() {
msg = unescapeFormData(chunks.split('=')[1]);
exchange.publish('', {body: msg});
res.statusCode = 303;
res.setHeader('Location', '/');
res.end();
});
}
else {
res.statusCode = 404;
res.end("This is not the page you were looking for.");
}
});
serv.listen(port, function() {
console.log("Listening on port " + port);
});
}

console.log("Starting ... AMQP URL: " + rabbitUrl());
var conn = amqp.createConnection({url: rabbitUrl()});
conn.on('ready', setup);

// ---- helpers

function openHtml(res) {
res.write("<html><head><title>Node.JS / RabbitMQ demo</title></head><body>");
}

function closeHtml(res) {
res.end("</body></html>");
}

function writeMessages(res) {
res.write('<h2>Messages</h2>');
res.write('<ol>');
for (i in messages) {
res.write('<li>' + messages[i] + '</li>');
}
res.write('</ol>');
}

function writeForm(res) {
res.write('<form method="post">');
res.write('<input name="data"/><input type="submit"/>');
res.write('</form>');
}

function printEnv(res) {
res.statusCode = 200;
openHtml(res);
for (entry in process.env) {
res.write(entry + "=" + process.env[entry] + "<br/>");
}
closeHtml(res);
}

function unescapeFormData(msg) {
return unescape(msg.replace('+', ' '));
}
11 changes: 11 additions & 0 deletions package.json
@@ -0,0 +1,11 @@
{

"name":"node-srs-demo",
"author": "Michael Bridgen",
"version":"0.0.2",
"dependencies":{
"amqp":">= 0.1.0",
"sanitizer": "*"
}

}

0 comments on commit dbf951f

Please sign in to comment.