-
Notifications
You must be signed in to change notification settings - Fork 9
/
taskground.js
120 lines (102 loc) · 3.43 KB
/
taskground.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const SECRET = 'TASKGROUND_SECRET';
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, fs = require('fs')
, cookieParser = require('cookie-parser')
, bodyParser = require('body-parser')
, cookieSession = require('cookie-session')
, io = require('socket.io').listen(server,{ log: false })
, swig = require('swig')
, compression = require('compression')
, provider = require('./lib/mongo_provider')
, websockets = require('./lib/websockets')
, Cookies = require('cookie-session/node_modules/cookies')
, routes = require('./lib/routes').create({provider: provider});
// if development.lock doesn't exist enable production mode
if (!fs.existsSync('./development.lock')) {
app.settings.env = 'production';
}
// global view variables
app.set('appName', 'Taskground');
app.set('appMode', app.settings.env);
/**
* app configuration
*/
app.set('port', process.env.PORT || 3100);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
swig.setDefaults({ cache: false });
app.use(compression());
app.use(express.static(__dirname + '/public'));
app.use(bodyParser());
app.use(cookieParser());
app.use(cookieSession({ 'key': 'express.sid', 'secret' : SECRET }));
/**
* connect data provider
*/
provider.connect('mongodb://localhost:27017/taskground', function(err, db) {
if (err) throw err;
/******************
***** routes *****
******************/
app.get('/', routes.noAuthMiddleware, routes.getHome);
app.get('/pricing', routes.noAuthMiddleware, routes.getPricing);
app.get('/support', routes.noAuthMiddleware, routes.getSupport);
app.route('/login').all(routes.noAuthMiddleware)
.get(routes.getLogin)
.post(routes.postLogin);
app.route('/signup').all(routes.noAuthMiddleware)
.get(routes.getSignup)
.post(routes.postSignup);
app.route('/recovery').all(routes.noAuthMiddleware)
.get(routes.getRecovery)
.post(routes.postRecovery);
app.get('/recovery/:token', routes.noAuthMiddleware, routes.getRecoveryLogin);
app.get('/logout', routes.getLogout);
// HTML client
app.get('/operator', routes.operatorOnly, routes.getOperator);
app.get('/mobile', routes.mobileOnly, routes.getMobile);
// paypal subscription
app.get('/subscribe/:type', routes.noAuthMiddleware, routes.subscribe);
app.get('/success', routes.noAuthMiddleware, routes.subscribeSuccess);
app.get('/paypal/ipn', routes.ipnNotification);
// 404 page not found handler
app.use(function(req, res, next) {
res.status(404);
res.render('404');
});
/**********************
***** websockets *****
**********************/
/**
* check session cookie to authorize the socket and accept the connection
*/
io.use(function(socket, next){
var cookies = new Cookies(socket.request, null, [SECRET]);
var session = cookies.get('express.sid', { 'key': 'express.sid', 'secret' : SECRET });
if (session) {
var session = new Buffer(session, 'base64');
socket.cookie = JSON.parse(session.toString());
next();
} else {
next(new Error('Authentication error'));
}
});
/**
* socket connection
*/
io.sockets.on('connection', function (socket) {
websockets.connection({
'socket': socket,
'io': io,
'provider': provider
});
});
/**
* start server
*/
server.listen(app.get('port'));
console.log('Server listening on port ' + app.get('port'));
});