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-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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2015-06-08 09:44:09 +02:00
|
|
|
// app.use(function(req, res, next) {
|
|
|
|
// console.log(req.session.user_id);
|
|
|
|
// next();
|
|
|
|
// });
|
2015-06-05 17:57:00 +02:00
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
// Load controllers
|
2015-06-25 17:21:19 +02:00
|
|
|
require('./lib/controllers')(app, { verbose: isDev });
|
2015-05-19 11:12:38 +02:00
|
|
|
|
|
|
|
// Load post to log data from user study
|
2015-06-25 17:21:19 +02:00
|
|
|
require('./lib/posts')(app, { verbose: isDev });
|
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-05-06 16:51:39 +02:00
|
|
|
var server_port, server_ip_address;
|
2015-06-25 17:21:19 +02:00
|
|
|
if ( isDev ) {
|
2015-05-06 16:32:00 +02:00
|
|
|
server_port = 4000;
|
2015-05-06 16:51:39 +02:00
|
|
|
server_ip_address = 'localhost';
|
2015-05-06 16:32:00 +02:00
|
|
|
} else {
|
2015-05-06 16:51:39 +02:00
|
|
|
// Openhift conf
|
2015-05-06 16:32:00 +02:00
|
|
|
server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
|
|
|
|
server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
// Start server
|
2015-06-25 17:21:19 +02:00
|
|
|
http.listen(server_port, server_ip_address, function() {
|
|
|
|
if (isDev)
|
|
|
|
console.log("Server ready : now listening " + server_ip_address + ":" + server_port);
|
2015-06-09 17:36:21 +02:00
|
|
|
});
|