summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--notes.txt6
-rw-r--r--protoype/app.js169
-rw-r--r--protoype/mydb.js18
-rw-r--r--protoype/router/driver.js6
-rw-r--r--protoype/router/index.js25
-rw-r--r--protoype/router/myplatform.js77
-rw-r--r--protoype/router/user.js69
7 files changed, 235 insertions, 135 deletions
diff --git a/notes.txt b/notes.txt
index 381be8b..dd286bc 100644
--- a/notes.txt
+++ b/notes.txt
@@ -26,3 +26,9 @@ $ node --debug-brk app.js
$ node-inspector &
http://0.0.0.0:8080/debug?port=5858
+[module.exports vs exports]
+- if you want your module to be of a specific object type (boolean, string, etc),
+ use module.exports; if you want your module to be a typical module instance, use exports
+- result of attaching properties to module.exports is akin (same) to attaching properties to exports
+- http://www.hacksparrow.com/node-js-exports-vs-module-exports.html
+
diff --git a/protoype/app.js b/protoype/app.js
index 450112e..0a698c2 100644
--- a/protoype/app.js
+++ b/protoype/app.js
@@ -1,24 +1,43 @@
/*
* kyle's nodejs server
*
- * ToDo: redis for active users
+ * by default app.settings.env is set to 'development' (process.env.ENV_VARIABLE)
+ * $ NODE_ENV=production node app.js
+ *
+ * ToDo:
+ * - redis for active users
+ * - load from db once, and refetch when necessary
+ * - if (verbose) log; can choose to use process.env and/or app.settings.env
*
*/
-var express = require('express');
-var RedisStore = require('connect-redis')(express);
-var dburl = 'localhost/nodejs1';
-var collections = ['users'];
-var db = require('mongojs').connect(dburl, collections);
var util = require('util');
var crypto = require('crypto')
-var driver = require('./router/driver.js');
-/* var activeusers = require('./activeusers'); */
-var connected_clients = {};
var path = require('path');
+var express = require('express');
+var RedisStore = require('connect-redis')(express);
+
+var db = require('./mydb.js');
+var driver = require('./router/driver.js');
+var myplatform = require('./router/myplatform.js');
+var user = require('./router/user.js');
+var index = require('./router/index.js');
+
var app = express();
+function deadend(req, res, next) {
+ util.log('[deadend] couldn\'t serve');
+ /* collect possible info here */
+ /* if (critical_wrong) then; throw new Error('da fuck this entity is doing!'); */
+ res.send(404, 'page not found');
+}
+
+function error_handler(err, req, res, next) { /* error handling, arity of 4 */
+ console.error(err.stack);
+ res.send(500, 'something broke!');
+}
+
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade'); /* no need to specify .jade extension */
@@ -30,134 +49,20 @@ app.configure(function() {
app.use(express.bodyParser()); /* creates req.body which req.param() uses */
app.use(express.cookieParser()); /* req.session can be populated with user defined vars */
app.use(express.session({ secret: "keyboard cat", store: new RedisStore() }));
- app.use(app.router);
+ app.use(app.router); /* when there's no match, we go static file handling below */
app.use(require('stylus').middleware(__dirname + '/public'));
- app.use(express.static(path.join(__dirname, 'public')));
-});
-
-app.post('/create', function(req, res) {
- db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline",
- vehicle: {make: req.param('make'), model: req.param('model'), year: req.param('year'),
- desc: req.param('desc')}, userinfo: {sig: req.param('sig')}, location: {loc: req.param('loc')},
- stats: {matches: 0, won: 0, lost: 0}},
- function(err, thing) {
- if (err || !thing)
- util.log('[create] error saving');
- else
- util.log('[create] successfully saved');
- });
- res.redirect('/');
+ app.use(express.static(path.join(__dirname, 'public'))); /* GET /stylesheets/style.css */
+ app.use(deadend); /* we get here if we couldn't serve */
});
-/*
- * test drive: curl -d 'tag=unclescotty&id=brotato' localhost:8081/login
- *
- */
-app.post('/login', function(req, res) {
- db.users.find({tag: req.param('tag')}, function(err, thing) {
- if (err || !thing || thing.length == 0) {
- util.log('[login] user does not exist');
- res.send('user does not exist\n', 403);
- }
- else {
- /* util.log('[login] retrived user: ' + util.inspect(thing)); */
- if (req.param('id') === thing[0].id) { /* insert md5 hashing here */
- util.log('[login] ' + thing[0].tag + ' authenticated');
- db.users.update({tag: req.param('tag')}, {$set: {status: 'online'}}, function(err, updated) {
- if (err || !updated)
- util.log('[login] failed to set status to online');
- });
- /* real deal? */
- connected_clients[thing[0].tag] = {ip: res.connection.myip, port: res.connection.myport};
- res.send('successfully logged in\n', 200);
- }
- else {
- util.log('[login] could not authenticate');
- res.send('could not authenticate\n', 401);
- }
- }
- });
-});
-
-/* /sys/do?get=activelist [list of active users]
- * /sys/do?get=entermatch&master=foo&slave=bar [enter master's match]
- * /sys/do?post=creatematch&master=foo&slave=bar [create match]
- *
- */
-app.get('/sys/:id([a-z]+)', function(req, res, next)
-{
- /* id contains routing token, req.query is a block/struct, key-val data structure containing GET params
- * id should be the main verb, action we want to do */
- util.log('[sys] route id aka action: ' + req.params.id);
- if (req.params.id === 'do')
- {
- var data = '';
- if (req.query.get != undefined)
- {
- util.log('[sys] get value: ' + req.query.get);
- if (req.query.get === 'activelist') {
- db.users.find({status: 'online'}, function(err, result) {
- if (err || !result)
- util.log('[sys] do?get=activelist failed or empty');
- else {
- for (var i = 0; i < result.length; i++) {
- data += result[i].tag + '\n';
- }
- res.send(data);
- }
- });
- }
- /* slave enters a match here */
- else if (req.query.get === 'entermatch' && req.query.master != undefined && req.query.master.length > 0 &&
- req.query.slave != undefined && req.query.slave.length > 0) {
- util.log('[sys] get: ' + req.query.slave + ' entered ' + req.query.master + '\'s match');
- res.send('entered match\n');
- }
- else
- next();
- }
- else if (req.query.post != undefined) /* post here refers to a param client would assign */
- {
- util.log('[sys] client post value: ' + req.query.post);
- if (req.query.post === 'creatematch' && req.query.master != undefined && req.query.master.length > 0 &&
- req.query.slave != undefined && req.query.slave.length > 0) { /* master creates a match */
- /* how the fuck do I get a hold of slave? */
- // invite slave, wait for reply, if accepted, keep that state!
- // respond to master
- util.log('[sys] post: ' + req.query.slave + ' accepted match');
- res.send('match accepted\n');
- } else {
- util.logger('[sys] unrecognized client data post');
- next();
- }
- }
- else
- {
- util.log('[sys] undefined GET params');
- next();
- }
- }
- else
- {
- /* unknown action */
- next();
- }
-});
-
-app.get('/', function(req, res) {
- db.users.find(function(err, items) {
- if (err || !items || items.length == 0)
- util.log('[index pn] nothing in db or error retrieving');
-
- res.render('index', {
- title: 'Challenger 2.0', /* might use app.locals */
- users: items
- });
- });
-});
+app.post('/create', user.create_post);
+app.post('/login', user.login_post);
+app.get('/sys/:id([a-z]+)', myplatform.system);
/* routing to handlers that can drive the server's functionality */
-app.get('/newuser', driver.newuser);
+app.get('/create', driver.create_get);
+
+app.get('/', index.root);
app.listen(8081, function() {
util.log(util.format('[server] listening on port %d in %s mode', this.address().port, app.settings.env));
diff --git a/protoype/mydb.js b/protoype/mydb.js
new file mode 100644
index 0000000..4a58580
--- /dev/null
+++ b/protoype/mydb.js
@@ -0,0 +1,18 @@
+/*
+ * db.js
+ *
+ * database include file
+ *
+ */
+
+var dburl = 'localhost/nodejs1';
+var collections = ['users'];
+var db = require('mongojs').connect(dburl, collections);
+
+
+/* this has to be 'module.exports' so that when this module is require'd the
+ * exported object that in this case is 'db' will be a reference instead of an
+ * undefined object
+ */
+module.exports = db;
+
diff --git a/protoype/router/driver.js b/protoype/router/driver.js
index 05e1039..5e79856 100644
--- a/protoype/router/driver.js
+++ b/protoype/router/driver.js
@@ -1,8 +1,8 @@
-/* handlers user to drive
+/* handlers used to drive
*
*/
-function newuser(req, res) {
+function create_get(req, res) {
res.send('<!DOCTYPE html>\n' +
'<html>\n' +
'<body>\n' +
@@ -21,5 +21,5 @@ function newuser(req, res) {
'</html>\n');
}
-exports.newuser = newuser;
+exports.create_get = create_get;
diff --git a/protoype/router/index.js b/protoype/router/index.js
new file mode 100644
index 0000000..d7da788
--- /dev/null
+++ b/protoype/router/index.js
@@ -0,0 +1,25 @@
+/*
+ * index.js
+ *
+ * main route
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+function root(req, res) {
+ db.users.find(function(err, items) {
+ if (err || !items || items.length == 0)
+ util.log('[index pn] nothing in db or error retrieving');
+
+ res.render('index', {
+ title: 'Challenger 2.0', /* might use app.locals */
+ users: items
+ });
+ });
+}
+
+/* we're exporting object of type module, so exports is enough */
+exports.root = root;
+
diff --git a/protoype/router/myplatform.js b/protoype/router/myplatform.js
new file mode 100644
index 0000000..ef189aa
--- /dev/null
+++ b/protoype/router/myplatform.js
@@ -0,0 +1,77 @@
+/*
+ * myplatform.js
+ *
+ * main inerface used for the server platform
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+/* /sys/do?get=activelist [list of active users]
+ * /sys/do?get=entermatch&master=foo&slave=bar [enter master's match]
+ * /sys/do?post=creatematch&master=foo&slave=bar [create match]
+ *
+ */
+function system(req, res, next)
+{
+ /* id contains routing token, req.query is a block/struct, key-val data structure containing GET params
+ * id should be the main verb, action we want to do */
+ util.log('[sys] route id aka action: ' + req.params.id);
+ if (req.params.id === 'do')
+ {
+ var data = '';
+ if (req.query.get != undefined)
+ {
+ util.log('[sys] get value: ' + req.query.get);
+ if (req.query.get === 'activelist') {
+ db.users.find({status: 'online'}, function(err, result) {
+ if (err || !result)
+ util.log('[sys] do?get=activelist failed or empty');
+ else {
+ for (var i = 0; i < result.length; i++) {
+ data += result[i].tag + '\n';
+ }
+ res.send(data);
+ }
+ });
+ }
+ /* slave enters a match here */
+ else if (req.query.get === 'entermatch' && req.query.master != undefined && req.query.master.length > 0 &&
+ req.query.slave != undefined && req.query.slave.length > 0) {
+ util.log('[sys] get: ' + req.query.slave + ' entered ' + req.query.master + '\'s match');
+ res.send('entered match\n');
+ }
+ else
+ next();
+ }
+ else if (req.query.post != undefined) /* post here refers to a param client would assign */
+ {
+ util.log('[sys] client post value: ' + req.query.post);
+ if (req.query.post === 'creatematch' && req.query.master != undefined && req.query.master.length > 0 &&
+ req.query.slave != undefined && req.query.slave.length > 0) { /* master creates a match */
+ /* how the fuck do I get a hold of slave? */
+ // invite slave, wait for reply, if accepted, keep that state!
+ // respond to master
+ util.log('[sys] post: ' + req.query.slave + ' accepted match');
+ res.send('match accepted\n');
+ } else {
+ util.logger('[sys] unrecognized client data post');
+ next();
+ }
+ }
+ else
+ {
+ util.log('[sys] undefined GET params');
+ next();
+ }
+ }
+ else
+ {
+ /* unknown action */
+ next();
+ }
+}
+
+exports.system = system;
+
diff --git a/protoype/router/user.js b/protoype/router/user.js
new file mode 100644
index 0000000..4215054
--- /dev/null
+++ b/protoype/router/user.js
@@ -0,0 +1,69 @@
+/*
+ * user.js
+ *
+ * routing handlers for a user
+ *
+ */
+
+var util = require('util');
+var db = require('../mydb.js');
+
+var connected_clients = {};
+
+function create_get(req, res, next) {
+}
+
+function create_post(req, res, next) {
+ db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline",
+ vehicle: {make: req.param('make'), model: req.param('model'), year: req.param('year'),
+ desc: req.param('desc')}, userinfo: {sig: req.param('sig')}, location: {loc: req.param('loc')},
+ stats: {matches: 0, won: 0, lost: 0}},
+ function(err, thing) {
+ if (err || !thing)
+ util.log('[create] error saving');
+ else
+ util.log('[create] successfully saved');
+ });
+ res.redirect('/');
+}
+
+function login_get(req, res, next) {
+}
+
+/*
+ * test drive: curl -d 'tag=unclescotty&id=brotato' localhost:8081/login
+ *
+ */
+function login_post(req, res, next) {
+ db.users.find({tag: req.param('tag')}, function(err, thing) {
+ if (err || !thing || thing.length == 0) {
+ util.log('[login] user does not exist');
+ res.send('user does not exist\n', 403);
+ }
+ else {
+ /* util.log('[login] retrived user: ' + util.inspect(thing)); */
+ if (req.param('id') === thing[0].id) { /* insert md5 hashing here */
+ util.log('[login] ' + thing[0].tag + ' authenticated');
+ db.users.update({tag: req.param('tag')}, {$set: {status: 'online'}}, function(err, updated) {
+ if (err || !updated)
+ util.log('[login] failed to set status to online');
+ });
+ /* real deal? */
+ connected_clients[thing[0].tag] = {ip: res.connection.myip, port: res.connection.myport};
+ res.send('successfully logged in\n', 200);
+ }
+ else {
+ util.log('[login] could not authenticate');
+ res.send('could not authenticate\n', 401);
+ }
+ }
+ });
+}
+
+module.exports = {
+ create_get: create_get,
+ create_post: create_post,
+ login_get: login_get,
+ login_post: login_post
+};
+