Skip to content
Browse files

Merge branch 'master' of github.com:coolaj86/node-examples-coolaj86

  • Loading branch information...
2 parents 6eaddaf + b1db699 commit f8737b51ae2826fc64c2e64affa1afc8324ab58d @coolaj86 committed Oct 21, 2010
View
1 webapps/README.md
@@ -0,0 +1 @@
+For each directory under `./vhosts`, a webapp is created on the vhost specified.
View
34 webapps/app.js
@@ -0,0 +1,34 @@
+var connect = require('connect'),
+ fs = require('fs'),
+ path = require('path'),
+ apps = [],
+ dirs = fs.readdirSync("./vhosts/");
+
+apps.push(connect.staticProvider(__dirname + '/public'));
+
+// TODO use futures to asyncify
+dirs.forEach(function (dir) {
+ dir = __dirname + '/vhosts/' + dir;
+ var file = dir + '/app.js',
+ app = dir + '/app',
+ stats;
+
+ try {
+ stats = fs.statSync(file);
+ } catch(e) {
+ console.log(file + " doesn't exist");
+ return;
+ }
+
+ if (stats.isFile()) {
+ console.log("loading middleware '" + app + "'");
+ apps.push(require(app));
+ }
+});
+
+apps.push(function(req, res){
+ res.writeHead(200, {});
+ res.end('local (vhost)');
+});
+
+module.exports = connect.createServer.apply(connect, apps);
View
5 webapps/config.js
@@ -0,0 +1,5 @@
+module.exports = {
+ port: 80,
+ user: "nobody",
+ env: "development"
+}
View
105 webapps/vhosts/dd-wrt.coolaj86.info/app.js
@@ -0,0 +1,105 @@
+// curl -d '{"location":"apt_5"}' -i -X POST http://dd-wrt.coolaj86.info/imalive -H "Content-Type: application/json"
+var fs = require('fs'),
+ sys = require('sys'),
+ connect = require('connect'),
+ url = require('url'),
+ rest,
+ server,
+ MDB = {};
+
+function retract(obj, props) {
+ var nobj = {};
+
+ obj = obj || {};
+ props.forEach(function (keyname) {
+ nobj[keyname] = obj[keyname];
+ });
+ return nobj;
+}
+
+rest = function (app) {
+ app.get('/routers', function(req, res) {
+ try {
+ var result = [];
+
+ req.params = url.parse(req.url, true).query;
+
+ Object.keys(MDB).forEach(function (item, key, collection) {
+ if (MDB[item]) {
+ result.push(MDB[item]);
+ }
+ });
+ result = JSON.stringify(result);
+
+ sys.puts('0there');
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ if (req.params && req.params.callback) {
+ result = req.params.callback + '(' + result + ')';
+ }
+ sys.puts('1there');
+ res.end(result);
+ sys.puts('2there');
+ } catch(e) {}
+ });
+
+ app.get('/imalive/:location', function(req, res) {
+ try {
+ var result = '{"success": "yay"}', item = {};
+
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ if (!req.params || !req.params.location) {
+ res.end('{err: "sucks"}');
+ return;
+ }
+ sys.puts('here0');
+ item.location = req.params.location;
+ item.remoteAddress = req.socket.remoteAddress;
+ MDB[item.remoteAddress] = item;
+
+ sys.puts('here1');
+ res.write(result);
+ sys.puts('here2');
+ res.end();
+ sys.puts('here3');
+ } catch(e) {}
+ });
+
+ app.post('/imalive', function(req, res) {
+ var result = '{"success": "yay"}', item;
+
+ console.log(sys.inspect(req.socket.remoteAddress));
+
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ if (!req.body || !req.body.location) {
+ res.end('{err: "sucks"}');
+ return;
+ }
+ item = retract(req.body, ["local_ip", "external_ip", "ethernet_mac", "location"]);
+ item.remoteAddress = req.socket.remoteAddress;
+ MDB[item.remoteAddress] = item;
+
+ res.end(result);
+ });
+};
+
+server = connect.createServer(
+ // decode html forms
+ connect.bodyDecoder(),
+ // REST API
+ connect.router(rest),
+ // images, css, etc
+ connect.staticProvider(__dirname + '/public'),
+ // All other request redirect to index
+ function(req, res){
+ var emitter = fs.createReadStream(__dirname + "/public/index.html", {encoding: 'utf8'});
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ emitter.on('data', function(data) {
+ res.write(data);
+ });
+ emitter.on('end', res.end);
+ }
+);
+
+module.exports = connect.createServer(
+ connect.vhost('dd-wrt.coolaj86.info', server)
+);
View
16 webapps/vhosts/dd-wrt.coolaj86.info/public/index.html
@@ -0,0 +1,16 @@
+<html>
+ <head>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ <script>
+ $(function () {
+ $.getJSON('/routers/?callback=?', {}, function (data) {
+ alert(JSON.stringify(data));
+ });
+ });
+ </script>
+ </head>
+ <body>
+ make request
+ get routers
+ </body>
+</html>
View
1 webapps/vhosts/dd-wrt.coolaj86.info/routers
@@ -0,0 +1 @@
+[{"location":"local_apt_5","remoteAddress":"109.169.56.223"}]

0 comments on commit f8737b5

Please sign in to comment.
Something went wrong with that request. Please try again.