3d-interface/server.js

108 lines
2.6 KiB
JavaScript
Raw Normal View History

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');
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
var isDev = app.get('env') === 'development';
2015-05-05 16:30:51 +02:00
app.set('view engine', 'jade');
app.set('trust proxy', 1);
2015-05-05 11:56:35 +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-07-02 14:30:31 +02:00
// Log connection
2015-07-03 13:27:05 +02:00
Log.request(req, res);
2015-07-02 14:30:31 +02:00
2015-05-05 11:56:35 +02:00
res.locals.title = "3DUI";
res.locals.urls = urls;
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.
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
}
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-05-06 16:51:39 +02:00
var server_port, server_ip_address;
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
http.listen(server_port, server_ip_address, function() {
2015-07-03 13:27:05 +02:00
Log.ready("Now listening " + server_ip_address + ":" + server_port);
2015-06-09 17:36:21 +02:00
});