Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Created March 30, 2014 13:53
Show Gist options
  • Select an option

  • Save ifukazoo/9873098 to your computer and use it in GitHub Desktop.

Select an option

Save ifukazoo/9873098 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
var http = require('http');
var url = require('url');
var fs = require('fs');
var mime = require('mime');
var exec = require('child_process').exec;
var port = 20080;
var db = {};
var doGet = function(req, res) {
var pathName = url.parse(req.url)['pathname'];
if (pathName === '/last') {
doGetLastFile(res);
} else {
if (pathName === '/') {
pathName += 'index.html';
}
pathName = '.' + pathName;
fs.exists(pathName, function(exists) {
if (exists) {
doGetFile(pathName, res);
} else {
doGetData(req.headers['user-agent'], res);
}
});
}
};
var doGetFile = function(path, res) {
fs.readFile(path, function(e, content) {
if (e) {
res.writeHead(404, e.message,
{ 'Content-Length': 0, 'Connection': 'close'});
} else {
res.writeHead(200, {
'Content-Length': content.length,
'Content-Type': mime.lookup(path),
'Connection': 'close'});
res.write(content);
}
res.end();
});
};
var doGetData = function(key, res) {
var data = db[key];
if (data) {
var json = JSON.stringify(data);
res.writeHead(200, {
'Content-Length': Buffer.byteLength(json),
'Content-Type': 'application/json; charset=utf-8',
'Connection': 'close'});
console.log(json);
res.write(json);
} else {
res.writeHead(404, { 'Content-Length': 0, 'Connection': 'close'});
}
res.end();
};
var doGetLastFile = function(res) {
var data = db['last_file'];
if (data) {
res.writeHead(200, {
'Content-Length': data.length,
'Content-Type': mime.lookup(data),
'Connection': 'close'});
res.write(data);
} else {
res.writeHead(404, { 'Content-Length': 0, 'Connection': 'close'});
}
res.end();
};
var doPost = function(req, res) {
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
var api = url.parse(req.url)['pathname'];
console.log(api);
switch (api) {
case '/data':
try {
var json = JSON.parse(data);
console.log(json);
db[req.headers['user-agent']] = json;
} catch (e) {
res.writeHead(400, e.message,
{ 'Content-Length': 0, 'Connection': 'close'});
} finally {
res.end();
}
break;
case '/file':
console.log(data);
db['last_file'] = data;
res.end();
break;
default:
res.end();
break;
}
});
};
var server = http.createServer(function(req, res) {
if (req.method === 'GET') {
console.log('GET');
doGet(req, res);
} else if (req.method === 'POST') {
console.log('POST');
doPost(req, res);
}
});
server.on('clientError', function(e) {
console.log('Client Error: ', e.message);
});
server.on('error', function(e) {
console.log('Server Error: ', e.message);
});
exec('ifconfig |grep -1 "^eth0" |grep inet', function(err, stdout, stderr) {
var result = stdout.match(/(\d{1,3}\.){3}\d{1,3}/);
server.listen(port, function() {
console.log('ip = ' + result[0]);
console.log('port = ' + port);
console.log('listening ...');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment