forked from jorgearanda/fish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
135 lines (109 loc) · 4.45 KB
/
app.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
/*jshint -W024 */
var express = require('express');
var http = require('http');
var logger = require('winston');
var mongoose = require('mongoose');
var MongoStore = require('connect-mongo')(express);
var path = require('path');
var socketio = require('socket.io');
var access = require('./middlewares/access');
var config = require('./config');
var engine = require('./engine/engine');
var experimenters = require('./routes/experimenters');
var microworlds = require('./routes/microworlds');
var runs = require('./routes/runs');
var sessions = require('./routes/sessions');
var isUser = access.isUser;
var authenticate = access.authenticate;
var app = exports.app = express();
app.configure(function() {
logger.cli();
logger.add(logger.transports.File, {
filename: 'fish.log',
handleExceptions: false
});
if (process.env.NODE_ENV === 'test') {
logger.remove(logger.transports.Console);
}
if (app.settings.env === 'development') {
process.env.NODE_ENV = 'development';
app.use(express.logger('dev'));
app.use(express.errorHandler());
} else if (app.settings.env === 'production') {
var loggerStream = {
write: function (message) { logger.info(message.slice(0, -1)); }
};
app.use(express.logger({ stream: loggerStream }));
}
app.set('port', process.env.PORT || 8080);
mongoose.connect(config.db[app.settings.env]);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('life is better under the sea'));
app.use(express.session({
secret: 'life is better under the sea',
store: new MongoStore({ mongoose_connection: mongoose.connections[0] }),
cookie: { maxAge: null }
}));
app.use(app.router);
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/bower', express.static(path.join(__dirname, 'bower_components')));
});
///////////////////////////////////////////////////////////////////////////////
// //
// Resources //
// //
///////////////////////////////////////////////////////////////////////////////
app.get('/', function (req, res) { res.render('participant-access.jade'); });
app.get('/new-welcome', function (req, res) { res.render('participant-access.jade'); });
app.get('/admin', function (req, res) { res.render('admin.jade'); });
app.get('/ping', function (req, res) { res.send('pong'); }); // Sanity check
app.post('/sessions', sessions.createSession);
app.post('/participant-sessions', sessions.participantSession);
app.get('/a/:accountId', authenticate, function (req, res) {
res.render('dashboard.jade');
});
app.get('/a/:accountId/dashboard', authenticate, function (req, res) {
res.render('dashboard.jade');
});
app.get('/a/:accountId/microworlds/:microworldId', authenticate, function (req, res) {
res.render('microworld.jade');
});
app.get('/a/:accountId/new/microworld', authenticate, function (req, res) {
res.render('microworld.jade');
});
app.get('/a/:accountId/runs/:runId', authenticate, function (req, res) {
res.render('run-results.jade');
});
app.get('/a/:accountId/profile', experimenters.displayProfileUpdate);
app.get('/fish', function (req, res) {
res.render('fish.jade');
});
app.get('/microworlds', isUser, microworlds.list);
app.get('/microworlds/:id', isUser, microworlds.show);
app.post('/microworlds', isUser, microworlds.create);
app.put('/microworlds/:id', isUser, microworlds.update);
app.delete('/microworlds/:id', isUser, microworlds.delete);
app.get('/runs', isUser, runs.list);
app.get('/runs/:id', isUser, runs.show);
app.post('/experimenters', experimenters.create);
app.put('/experimenters/:id', isUser, experimenters.update);
var server = http.createServer(app);
var io = exports.io = socketio.listen(server, {
logger: {
debug: logger.debug,
info: logger.info,
warn: logger.warn,
error: logger.error
}
});
var ioAdmin = exports.ioAdmin = io.of('/admin');
engine.engine(io, ioAdmin);
server.listen(app.get('port'), function () {
logger.info('Fish server listening on port ' + app.get('port'));
});