Permalink
Browse files

forgot to add the service scripts

  • Loading branch information...
1 parent 93e9242 commit 8fb1a440788af8e8aed6f0d4d1bc4618e493deff @substack committed Mar 30, 2012
Showing with 71 additions and 0 deletions.
  1. +14 −0 auth.js
  2. +28 −0 bouncer.js
  3. +29 −0 web.js
View
14 auth.js
@@ -0,0 +1,14 @@
+var airport = require('airport');
+var air = airport('localhost', 7000);
+
+var users = {
+ beep : { password : 'boop' },
+ bilbo : { password : 'hobbitz' },
+};
+
+air(function (remote, conn) {
+ this.check = function (user, pass, cb) {
+ var u = users[user];
+ cb(u && u.password === pass);
+ };
+}).listen('auth');
View
28 bouncer.js
@@ -0,0 +1,28 @@
+var seaport = require('seaport');
+var ports = seaport.createServer();
+var pick = require('deck').pick;
+ports.listen(7000);
+
+var bouncy = require('bouncy');
+
+var server = bouncy(function (req, bounce) {
+ function error (msg) {
+ var res = bounce.respond();
+ res.writeHead(404, { 'content-type' : 'text/plain' })
+ res.end(msg + '\r\n');
+ }
+
+ var parts = (req.headers.host || '').split('.');
+ if (parts[parts.length - 2] === 'web') {
+ var v = parts.slice(0, -2).join('.');
+ var ps = ports.query('web@' + v);
+ if (ps.length === 0) error('no matching services are running');
+ else bounce(pick(ps));
+ }
+ else {
+ var ps = ports.query('web');
+ if (ps.length === 0) error('no web servers are running');
+ else bounce(pick(ps));
+ }
+});
+server.listen(8080);
View
29 web.js
@@ -0,0 +1,29 @@
+var airport = require('airport');
+var seaport = require('seaport');
+
+var ports = seaport.connect('localhost', 7000);
+var air = airport(ports);
+
+var up = { auth : air.connect('auth') };
+
+var http = require('http');
+var server = http.createServer(function (req, res) {
+ if (req.method === 'POST') {
+ var data = '';
+ req.on('data', function (buf) { data += buf });
+ req.on('end', function () {
+ var r = JSON.parse(data);
+ up.auth(function (remote) {
+ remote.check(r.user, r.pass, function (ok) {
+ res.write(ok ? 'OK' : 'NOT OK');
+ res.end('\n');
+ });
+ });
+ });
+ }
+ else res.end(process.argv[2] + '\n');
+});
+
+ports.service('web@0.0.0', function (port, ready) {
+ server.listen(port, ready);
+});

0 comments on commit 8fb1a44

Please sign in to comment.