summaryrefslogtreecommitdiffstats
path: root/protoype/app.js
blob: 73b0aa87b425ccd8ea67ec6cc8703f582074b2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
});