summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-06-29 00:31:15 -0500
committerKyle Kaminski <kyle@kkaminsk.com>2012-06-29 00:31:15 -0500
commit14f7c610cbf281b5f5776b84b452baf5dfc31f52 (patch)
treee83b24f857bb67fe7c59dfcb5882d8ce2f3439b1
downloadfubar-14f7c610cbf281b5f5776b84b452baf5dfc31f52.tar.gz
fubar-14f7c610cbf281b5f5776b84b452baf5dfc31f52.tar.bz2
fubar-14f7c610cbf281b5f5776b84b452baf5dfc31f52.zip
initial commit
-rw-r--r--pnhandler.js45
-rw-r--r--route.js38
-rw-r--r--tinyserver.js52
3 files changed, 135 insertions, 0 deletions
diff --git a/pnhandler.js b/pnhandler.js
new file mode 100644
index 0000000..4123049
--- /dev/null
+++ b/pnhandler.js
@@ -0,0 +1,45 @@
+/*
+ * pnhandler.js
+ *
+ * group of methods that handles different pathnames
+ */
+
+var util = require('util');
+
+function noop(query, res) {
+ util.log('[pnhandler] handling ' + query.pathname +
+ ', ok who the fuck is messing with us?');
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end();
+}
+
+function register(query, res) {
+ util.log('[pnhandler] handling ' + query.pathname);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end();
+}
+
+function login(query, res) {
+ util.log('[pnhandler] handling ' + query.pathname);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end();
+}
+
+function poll(query, res) {
+ util.log('[pnhandler] handling ' + query.pathname);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end();
+}
+
+function upload(query, res) {
+ util.log('[pnhandler] handling ' + query.pathname);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end();
+}
+
+exports.noop = noop;
+exports.register = register;
+exports.login = login;
+exports.poll = poll;
+exports.upload = upload;
+
diff --git a/route.js b/route.js
new file mode 100644
index 0000000..e8f614f
--- /dev/null
+++ b/route.js
@@ -0,0 +1,38 @@
+/*
+ * route.js
+ *
+ * used for handling pathnames in url query
+ */
+
+var util = require('util');
+
+var pnhandler = require('./pnhandler');
+
+/* list of handlers, not a global variable */
+var handle = {
+ '/': pnhandler.noop,
+ '/register': pnhandler.register,
+ '/login': pnhandler.login,
+ '/poll': pnhandler.poll,
+ '/upload': pnhandler.upload
+};
+
+function route(query, res) {
+ if (typeof handle[query.pathname] === 'function' ) {
+ handle[query.pathname](query, res);
+ } else {
+ util.log('[route] no handler found for ' + query.pathname);
+
+ res.writeHead(404, {'Content-Type': 'text/html'});
+ res.write('<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<body>\n' +
+ '\t404 Not found\n' +
+ '</body>\n' +
+ '</html>\n');
+ res.end();
+ }
+}
+
+exports.route = route;
+
diff --git a/tinyserver.js b/tinyserver.js
new file mode 100644
index 0000000..fd9d37d
--- /dev/null
+++ b/tinyserver.js
@@ -0,0 +1,52 @@
+var http = require('http');
+var util = require('util');
+var url = require('url');
+
+var router = require('./route');
+
+var serverConfig = {
+ port: 8080,
+ hostname: 'localhost' /* eth interface ip? */
+};
+
+/* current list of active users, key-value pairs */
+var activeUsers = [];
+
+/* this function is poked upon 'request' event */
+function requestListener(req, res) {
+ var datareq = '';
+
+ req.on('data', function(chunk) { datareq += chunk; } );
+ req.on('end', function() {
+ util.log('[reqlistener] finished recv data');
+ //console.log('data: %s', datareq.length == 0 ? 'empty' : datareq);
+
+ /* should be some logic, e.g. blueprint 1 */
+ util.log('[reqlistener] request kind: ' + req.method);
+ /* routing for pathname, figure out what request we have */
+ var urlquery = url.parse(req.url);
+ //console.log('[reqlistener] query dump:\n' + util.inspect(urlquery));
+ router.route(urlquery, res);
+ });
+ req.on('close', function() { util.log('[reqlistener] client terminated before we could respond'); });
+}
+
+var server = http.createServer(requestListener);
+server.listen(serverConfig.port, serverConfig.hostname, function() {
+ /* could use for proxy stuff? this function is called when listening event is emitted */
+});
+
+server.on('connection', function(socket) {
+ util.log('[server] new connection from ' + socket.remoteAddress + ':' + socket.remotePort
+ + ', read ' + socket.bytesRead + ' bytes');
+ //console.log(util.inspect(socket), false /* do not show non-enum props */, true);
+});
+
+server.on('close', function() {
+ util.log('[server] server shutdown');
+});
+
+server.on('clientError', function(exception) {
+ util.log('[server] ' + exception);
+});
+