This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial revision ported from CF example
- Loading branch information
Michael Bridgen
committed
Aug 26, 2011
0 parents
commit dbf951f
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.git | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node app.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('+', ' ')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
|
||
"name":"node-srs-demo", | ||
"author": "Michael Bridgen", | ||
"version":"0.0.2", | ||
"dependencies":{ | ||
"amqp":">= 0.1.0", | ||
"sanitizer": "*" | ||
} | ||
|
||
} |