summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-07-01 21:57:24 -0500
committerKyle Kaminski <kyle@kkaminsk.com>2012-07-01 21:57:24 -0500
commitb001224bb95996fa59269a5c119c6a9f2047f288 (patch)
tree2fc3b73f4fedb63f6ce9a0cc02ec35ef0864ceab
parent41946c6982647abec5d64b04323a081c73c43577 (diff)
downloadfubar-b001224bb95996fa59269a5c119c6a9f2047f288.tar.gz
fubar-b001224bb95996fa59269a5c119c6a9f2047f288.tar.bz2
fubar-b001224bb95996fa59269a5c119c6a9f2047f288.zip
start on prototype
-rw-r--r--notes.txt4
-rw-r--r--protoype/activeusers.js44
-rw-r--r--protoype/app.js102
-rw-r--r--protoype/package.json9
-rw-r--r--protoype/public/stylesheets/style.css9
-rw-r--r--protoype/router/driver.js19
-rw-r--r--protoype/views/index.jade7
-rw-r--r--protoype/views/layout.jade9
8 files changed, 203 insertions, 0 deletions
diff --git a/notes.txt b/notes.txt
index 8b7ed8f..3873d42 100644
--- a/notes.txt
+++ b/notes.txt
@@ -7,3 +7,7 @@ generates nice template dir structure to get started
# npm install -g jslint
$ jslint --sloppy --white <file.js>
+[make a copy of an array]
+var foo = [1, 2, 3];
+var bar = foo.slice(0);
+
diff --git a/protoype/activeusers.js b/protoype/activeusers.js
new file mode 100644
index 0000000..64df0d9
--- /dev/null
+++ b/protoype/activeusers.js
@@ -0,0 +1,44 @@
+/*
+ * activeusers.js
+ *
+ * list of active users, maps tag/user to a status string
+ */
+
+var redis = require('redis');
+var client = redis.createClient();
+var util = require('util');
+
+client.on('error', function(e) {
+ util.log('[activeusers] redis ' + e);
+});
+
+function isactive(tag) {
+ client.get(tag, function(err, reply) {
+ if (err || !reply)
+ return false;
+ else
+ return true;
+ });
+}
+
+function setstatus(tag, status) {
+ client.set(tag, status, redis.print);
+}
+
+function list() {
+ client.keys('*', function(err, replies) {
+ if (err || !replies)
+ util.log('[activeusers] could not get a list of users');
+ else
+ {
+ replies.forEach(function(reply, i) {
+ util.log('key ' + i + ': ' + reply);
+ });
+ }
+ });
+}
+
+exports.isactive = isactive;
+exports.setstatus = setstatus;
+exports.list = list;
+
diff --git a/protoype/app.js b/protoype/app.js
new file mode 100644
index 0000000..73b0aa8
--- /dev/null
+++ b/protoype/app.js
@@ -0,0 +1,102 @@
+/*
+ * kyle's nodejs server
+ *
+ * ToDo: redis for active users
+ *
+ */
+
+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 driver = require('./router/driver.js');
+/* var activeusers = require('./activeusers'); */
+
+var app = express.createServer();
+
+app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade'); /* no need to specify .jade extension */
+ app.set('view options', { pretty: true }); /* avoid html source all in on line or so */
+
+ app.use(express.logger('dev'));
+ app.use(express.favicon());
+ app.use(express.compress()); /* gzip */
+ 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(express.static(__dirname + '/public'));
+});
+
+app.post('/create', function(req, res) {
+ db.users.save({tag: req.param('tag'), id: req.param('id'), active: false, vehicle: '',
+ avatar: 'images/avatars/default.png', sig: '', location: '', races: 0},
+ function(err, thing) {
+ if (err || !thing)
+ util.log('[create] error saving');
+ else
+ util.log('[create] successfully saved');
+ });
+ res.redirect('/');
+});
+
+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] successfully logged in');
+ thing[0].active = true; /* this, or have redis? */
+ res.send('successfully logged in\n', 200);
+ }
+ else {
+ util.log('[login] could not authenticate');
+ res.send('could not authenticate\n', 401);
+ }
+ }
+ });
+});
+
+app.post('/poll:id([a-z]+)', function(req, res) {
+ db.users.save({tag: req.param('tag'), id: req.param('id'), active: false, vehicle: '',
+ avatar: 'images/avatars/default.png', sig: '', location: '', races: 0},
+ function(err, thing) {
+ if (err || !thing)
+ util.log('[create] error saving');
+ else
+ util.log('[create] successfully saved');
+ });
+ res.redirect('/');
+});
+
+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', {
+ locals: {
+ title: 'Challenger 2.0',
+ users: items
+ }
+ });
+ });
+});
+
+/* routing to handlers that can driver the server's functionality */
+app.get('/newuser', driver.newuser);
+
+app.listen(8081, function() {
+ console.log("listening on port %d in %s mode", this.address().port, this.settings.env);
+})
+.on('error', function(e) {
+ console.log('failed creating server, errno: ' + e.errno);
+});
+
diff --git a/protoype/package.json b/protoype/package.json
new file mode 100644
index 0000000..ce83489
--- /dev/null
+++ b/protoype/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.11"
+ , "jade": ">= 0.0.1"
+ }
+} \ No newline at end of file
diff --git a/protoype/public/stylesheets/style.css b/protoype/public/stylesheets/style.css
new file mode 100644
index 0000000..c4dfff7
--- /dev/null
+++ b/protoype/public/stylesheets/style.css
@@ -0,0 +1,9 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
+
diff --git a/protoype/router/driver.js b/protoype/router/driver.js
new file mode 100644
index 0000000..2eeadbb
--- /dev/null
+++ b/protoype/router/driver.js
@@ -0,0 +1,19 @@
+/* handlers user to drive
+ *
+ */
+
+function newuser(req, res) {
+ res.send('<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<body>\n' +
+ '<form method="post" action="/create">\n' +
+ ' Tag: <input type="text" name="tag" /><br />\n' +
+ ' ID: <input type="text" name="id" /><br />\n' +
+ ' <input type="submit" value="add" />\n' +
+ '</form>\n' +
+ '</body>\n' +
+ '</html>\n');
+}
+
+exports.newuser = newuser;
+
diff --git a/protoype/views/index.jade b/protoype/views/index.jade
new file mode 100644
index 0000000..57cde6f
--- /dev/null
+++ b/protoype/views/index.jade
@@ -0,0 +1,7 @@
+h1= title
+p Welcome to #{title}. Users in the portal:
+
+h3 User list:
+ul
+ - each user in users
+ li= user.tag
diff --git a/protoype/views/layout.jade b/protoype/views/layout.jade
new file mode 100644
index 0000000..c7139b7
--- /dev/null
+++ b/protoype/views/layout.jade
@@ -0,0 +1,9 @@
+!!!
+html
+ head
+ title= title
+ meta(http-equiv='Content-Type', content='text/html; charset=UTF-8')
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ // body gets passed in here from *.jade? the '!=' outputs unescaped
+ body!= body
+