summaryrefslogtreecommitdiffstats
path: root/cholerab/knut/index.js
diff options
context:
space:
mode:
authortv <tv@xso>2011-08-13 14:56:27 +0200
committertv <tv@xso>2011-08-13 14:56:27 +0200
commitb50c069f0f824f47234d29d8784489a31c0b5d40 (patch)
tree3b05bbc583007550f402cfd77e9ffb91e08656b0 /cholerab/knut/index.js
parentdf6c500d626358f6f7d6fa231ec21b85b0a42319 (diff)
cholerab knut: initial commit
Diffstat (limited to 'cholerab/knut/index.js')
-rwxr-xr-xcholerab/knut/index.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/cholerab/knut/index.js b/cholerab/knut/index.js
new file mode 100755
index 00000000..e7278678
--- /dev/null
+++ b/cholerab/knut/index.js
@@ -0,0 +1,63 @@
+#! /usr/bin/env node
+
+var host = '0.0.0.0';
+var port = 42101;
+
+var pipe = '/tmp/krebscode.painload.cholerab.ttycnser.' + process.env.LOGNAME;
+
+var fs = require('fs');
+var http = require('http');
+var slurp = require('./src/io/slurp');
+var spawn = require('child_process').spawn;
+
+var plugs = process.argv.slice(2);
+
+http.createServer(function (req, res) {
+ return slurp(req, function (data) {
+ try {
+ var message = JSON.parse(data);
+ } catch (exn) {
+ console.error(exn.stack);
+ };
+ if (message) {
+ plugs.forEach(function (plug) {
+
+ var env = JSON.parse(JSON.stringify(process.env));
+ Object.keys(message).forEach(function (key) {
+ env[key] = message[key];
+ });
+
+ var child = spawn(__dirname + '/plugs/' + plug + '/index', [], {
+ env: env
+ });
+
+ child.stdout.on('data', function (data) {
+ console.log(plug, 'stdout:', data.toString());
+ });
+
+ child.stderr.on('data', function (data) {
+ console.log(plug, 'stderr:', data.toString());
+ });
+
+ child.on('exit', function (code) {
+ console.log(plug, 'exit:', code);
+ if (code === 0) {
+ res.writeHead(200, { 'Content-Length': 0 });
+ res.end();
+ } else {
+ res.writeHead(500, { 'Content-Length': 0 });
+ res.end();
+ };
+ });
+
+ });
+ } else {
+ res.writeHead(400, 'You are made of stupid!', {
+ 'Content-Length': 0
+ });
+ res.end();
+ };
+ });
+}).listen(port, host, function () {
+ console.log('Serving HTTP on', host, 'port', port);
+});