3d-interface/server/server.js

129 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

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