2015-05-05 11:56:35 +02:00
|
|
|
var http = require('http');
|
|
|
|
var express = require('express');
|
2015-05-05 16:30:51 +02:00
|
|
|
var jade = require('jade');
|
2015-05-18 10:48:37 +02:00
|
|
|
var pg = require('pg');
|
|
|
|
|
2015-06-26 10:50:01 +02:00
|
|
|
var favicon = require('serve-favicon');
|
|
|
|
|
2015-05-18 17:51:20 +02:00
|
|
|
// secret variables
|
|
|
|
var secret = require('./private');
|
2015-05-05 11:56:35 +02:00
|
|
|
|
|
|
|
var app = express();
|
2015-06-09 17:36:21 +02:00
|
|
|
|
|
|
|
// Socket.io initialization
|
|
|
|
var http = require('http').Server(app);
|
|
|
|
var io = require('socket.io')(http);
|
|
|
|
require('./socket.js')(io);
|
|
|
|
|
2015-05-18 17:51:20 +02:00
|
|
|
var bodyParser = require('body-parser');
|
2015-05-19 10:14:06 +02:00
|
|
|
var session = require('cookie-session');
|
2015-05-18 17:51:20 +02:00
|
|
|
var cookieParser = require('cookie-parser');
|
2015-05-05 11:56:35 +02:00
|
|
|
var urls = require('./urls');
|
2015-07-03 13:27:05 +02:00
|
|
|
var Log = require('./lib/NodeLog.js');
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-06-25 17:21:19 +02:00
|
|
|
var isDev = app.get('env') === 'development';
|
|
|
|
|
2015-05-05 16:30:51 +02:00
|
|
|
app.set('view engine', 'jade');
|
2015-05-19 10:14:06 +02:00
|
|
|
app.set('trust proxy', 1);
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-05-19 10:14:06 +02:00
|
|
|
app.use(cookieParser(secret.secret));
|
2015-05-18 17:51:20 +02:00
|
|
|
app.use(session({
|
2015-05-19 11:56:47 +02:00
|
|
|
keys: [secret.secret]
|
2015-05-18 17:51:20 +02:00
|
|
|
}));
|
|
|
|
|
2015-05-18 15:33:14 +02:00
|
|
|
app.use(bodyParser.text());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
app.use(function(req, res, next) {
|
2015-07-02 15:33:13 +02:00
|
|
|
|
2015-09-14 10:39:15 +02:00
|
|
|
var start = Date.now();
|
|
|
|
|
|
|
|
res.on('finish', function() {
|
|
|
|
// Log connection
|
|
|
|
Log.request(req, res, Date.now() - start);
|
|
|
|
|
|
|
|
});
|
2015-07-02 14:30:31 +02:00
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
res.locals.title = "3DUI";
|
|
|
|
res.locals.urls = urls;
|
2015-06-05 17:57:00 +02:00
|
|
|
res.locals.session = req.session;
|
2015-05-05 11:56:35 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
// Set a cookie to know if already came. If not, France laws force to
|
|
|
|
// warn the user that the website uses cookies.
|
2015-05-19 10:14:06 +02:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if (req.cookies.alreadyCame) {
|
|
|
|
res.locals.alertCookie = false;
|
|
|
|
} else {
|
|
|
|
res.locals.alertCookie = true;
|
2015-05-19 11:12:38 +02:00
|
|
|
res.cookie('alreadyCame', true, {maxAge: 604800000}); // One week in ms
|
2015-05-19 10:14:06 +02:00
|
|
|
}
|
2015-09-28 11:00:52 +02:00
|
|
|
|
|
|
|
if (req.url.substr(0, 7) === '/static' || req.url === '/favicon.ico') {
|
|
|
|
req.static = true;
|
|
|
|
}
|
|
|
|
|
2015-05-19 10:14:06 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
// Load controllers
|
2015-07-07 09:27:03 +02:00
|
|
|
require('./lib/controllers')(app);
|
2015-05-19 11:12:38 +02:00
|
|
|
|
|
|
|
// Load post to log data from user study
|
2015-07-07 09:27:03 +02:00
|
|
|
require('./lib/posts')(app);
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
// Static files
|
2015-05-05 11:56:35 +02:00
|
|
|
app.use('/static', express.static('static'));
|
|
|
|
|
2015-06-26 10:50:01 +02:00
|
|
|
// Favicon
|
|
|
|
app.use(favicon(__dirname + '/static/ico/favicon.ico'));
|
|
|
|
|
2015-05-06 16:32:00 +02:00
|
|
|
// When error raised
|
2015-05-05 11:56:35 +02:00
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
if (err.status === 404) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
2015-05-05 16:30:51 +02:00
|
|
|
res.render('404.jade', res.locals, function(err, result) {
|
2015-05-05 11:56:35 +02:00
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-05-06 16:32:00 +02:00
|
|
|
// When route not found, raise not found
|
2015-05-05 11:56:35 +02:00
|
|
|
app.use(function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
2015-05-05 16:30:51 +02:00
|
|
|
res.render('404.jade', res.locals, function(err, result) {
|
2015-05-05 11:56:35 +02:00
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-06 16:52:27 +02:00
|
|
|
// Set ports and ip address
|
2015-08-28 21:34:29 +02:00
|
|
|
var serverPort, serverIpAddress;
|
2015-06-25 17:21:19 +02:00
|
|
|
if ( isDev ) {
|
2015-08-28 21:34:29 +02:00
|
|
|
serverPort = 4000;
|
2015-09-09 15:35:40 +02:00
|
|
|
serverIpAddress = 'localhost';
|
2015-05-06 16:32:00 +02:00
|
|
|
} else {
|
2015-05-06 16:51:39 +02:00
|
|
|
// Openhift conf
|
2015-08-28 21:34:29 +02:00
|
|
|
serverPort = process.env.OPENSHIFT_NODEJS_PORT || 8080;
|
|
|
|
serverIpAddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
|
2015-05-06 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
// Start server
|
2015-08-28 21:34:29 +02:00
|
|
|
http.listen(serverPort, serverIpAddress, function() {
|
|
|
|
Log.ready("Now listening " + serverIpAddress + ":" + serverPort);
|
2015-06-09 17:36:21 +02:00
|
|
|
});
|