Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit f8737b51ae2826fc64c2e64affa1afc8324ab58d 2 parents 6eaddaf + b1db699
AJ ONeal authored
1  webapps/README.md
Source Rendered
... ... @@ -0,0 +1 @@
  1 +For each directory under `./vhosts`, a webapp is created on the vhost specified.
34 webapps/app.js
... ... @@ -0,0 +1,34 @@
  1 +var connect = require('connect'),
  2 + fs = require('fs'),
  3 + path = require('path'),
  4 + apps = [],
  5 + dirs = fs.readdirSync("./vhosts/");
  6 +
  7 +apps.push(connect.staticProvider(__dirname + '/public'));
  8 +
  9 +// TODO use futures to asyncify
  10 +dirs.forEach(function (dir) {
  11 + dir = __dirname + '/vhosts/' + dir;
  12 + var file = dir + '/app.js',
  13 + app = dir + '/app',
  14 + stats;
  15 +
  16 + try {
  17 + stats = fs.statSync(file);
  18 + } catch(e) {
  19 + console.log(file + " doesn't exist");
  20 + return;
  21 + }
  22 +
  23 + if (stats.isFile()) {
  24 + console.log("loading middleware '" + app + "'");
  25 + apps.push(require(app));
  26 + }
  27 +});
  28 +
  29 +apps.push(function(req, res){
  30 + res.writeHead(200, {});
  31 + res.end('local (vhost)');
  32 +});
  33 +
  34 +module.exports = connect.createServer.apply(connect, apps);
5 webapps/config.js
... ... @@ -0,0 +1,5 @@
  1 +module.exports = {
  2 + port: 80,
  3 + user: "nobody",
  4 + env: "development"
  5 +}
105 webapps/vhosts/dd-wrt.coolaj86.info/app.js
... ... @@ -0,0 +1,105 @@
  1 +// curl -d '{"location":"apt_5"}' -i -X POST http://dd-wrt.coolaj86.info/imalive -H "Content-Type: application/json"
  2 +var fs = require('fs'),
  3 + sys = require('sys'),
  4 + connect = require('connect'),
  5 + url = require('url'),
  6 + rest,
  7 + server,
  8 + MDB = {};
  9 +
  10 +function retract(obj, props) {
  11 + var nobj = {};
  12 +
  13 + obj = obj || {};
  14 + props.forEach(function (keyname) {
  15 + nobj[keyname] = obj[keyname];
  16 + });
  17 + return nobj;
  18 +}
  19 +
  20 +rest = function (app) {
  21 + app.get('/routers', function(req, res) {
  22 + try {
  23 + var result = [];
  24 +
  25 + req.params = url.parse(req.url, true).query;
  26 +
  27 + Object.keys(MDB).forEach(function (item, key, collection) {
  28 + if (MDB[item]) {
  29 + result.push(MDB[item]);
  30 + }
  31 + });
  32 + result = JSON.stringify(result);
  33 +
  34 + sys.puts('0there');
  35 + res.writeHead(200, {'Content-Type': 'application/json'});
  36 + if (req.params && req.params.callback) {
  37 + result = req.params.callback + '(' + result + ')';
  38 + }
  39 + sys.puts('1there');
  40 + res.end(result);
  41 + sys.puts('2there');
  42 + } catch(e) {}
  43 + });
  44 +
  45 + app.get('/imalive/:location', function(req, res) {
  46 + try {
  47 + var result = '{"success": "yay"}', item = {};
  48 +
  49 + res.writeHead(200, {'Content-Type': 'application/json'});
  50 + if (!req.params || !req.params.location) {
  51 + res.end('{err: "sucks"}');
  52 + return;
  53 + }
  54 + sys.puts('here0');
  55 + item.location = req.params.location;
  56 + item.remoteAddress = req.socket.remoteAddress;
  57 + MDB[item.remoteAddress] = item;
  58 +
  59 + sys.puts('here1');
  60 + res.write(result);
  61 + sys.puts('here2');
  62 + res.end();
  63 + sys.puts('here3');
  64 + } catch(e) {}
  65 + });
  66 +
  67 + app.post('/imalive', function(req, res) {
  68 + var result = '{"success": "yay"}', item;
  69 +
  70 + console.log(sys.inspect(req.socket.remoteAddress));
  71 +
  72 + res.writeHead(200, {'Content-Type': 'application/json'});
  73 + if (!req.body || !req.body.location) {
  74 + res.end('{err: "sucks"}');
  75 + return;
  76 + }
  77 + item = retract(req.body, ["local_ip", "external_ip", "ethernet_mac", "location"]);
  78 + item.remoteAddress = req.socket.remoteAddress;
  79 + MDB[item.remoteAddress] = item;
  80 +
  81 + res.end(result);
  82 + });
  83 +};
  84 +
  85 +server = connect.createServer(
  86 + // decode html forms
  87 + connect.bodyDecoder(),
  88 + // REST API
  89 + connect.router(rest),
  90 + // images, css, etc
  91 + connect.staticProvider(__dirname + '/public'),
  92 + // All other request redirect to index
  93 + function(req, res){
  94 + var emitter = fs.createReadStream(__dirname + "/public/index.html", {encoding: 'utf8'});
  95 + res.writeHead(200, {'Content-Type': 'text/html'});
  96 + emitter.on('data', function(data) {
  97 + res.write(data);
  98 + });
  99 + emitter.on('end', res.end);
  100 + }
  101 +);
  102 +
  103 +module.exports = connect.createServer(
  104 + connect.vhost('dd-wrt.coolaj86.info', server)
  105 +);
16 webapps/vhosts/dd-wrt.coolaj86.info/public/index.html
... ... @@ -0,0 +1,16 @@
  1 +<html>
  2 + <head>
  3 + <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  4 + <script>
  5 + $(function () {
  6 + $.getJSON('/routers/?callback=?', {}, function (data) {
  7 + alert(JSON.stringify(data));
  8 + });
  9 + });
  10 + </script>
  11 + </head>
  12 + <body>
  13 + make request
  14 + get routers
  15 + </body>
  16 +</html>
1  webapps/vhosts/dd-wrt.coolaj86.info/routers
... ... @@ -0,0 +1 @@
  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.