/**
 * Project Oracle
 * TIE-13106 (TIE-13100) Project Work on Pervasive Systems
 */

var fs = require('fs');
var express = require('./node_modules/express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var bodyParser = require('body-parser');

var app = express();

// Load config if exists
var config = {};
var configFile = './config.js';
if (fs.existsSync(configFile)) {
    config = require(configFile);
}

// Envinronment settings
app.set('port', (config.port || process.env.PORT || 5000));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
  extended: true
}))

app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
@@ -210,24 +210,54 @@
 re('http');%0A
+var https = require('https');%0A
 var path = r
@@ -268,24 +268,24 @@
 re('path');%0A
-
 var bodyPars
@@ -478,16 +478,171 @@
 File);%0A%7D
+%0Avar useSSL = typeof config.useSSL != %22undefined%22 ? config.useSSL : true;%0Avar forcedSSL = typeof config.forcedSSL != %22undefined%22 ? config.forcedSSL : true;
 %0A%0A// Env
@@ -716,24 +716,93 @@
  %7C%7C 5000));%0A
+app.set('sslport', (config.sslport %7C%7C process.env.SSLPORT %7C%7C 5001));%0A
 app.set('vie
@@ -927,37 +927,8 @@
 ))%0A%0A
-app.get('/', routes.index);%0A%0A
 http
@@ -1005,16 +1005,21 @@
 Express 
+http 
 server l
@@ -1060,8 +1060,1173 @@
 '));%0A%7D);
+%0A%0A// Start https server for other environments than heroku%0Aif (process.env.NODE_ENV != 'heroku' && useSSL) %7B%0A%09if (config && config.sslOptions) %7B%0A%09%09https.createServer(config.sslOptions, app).listen(app.get('sslport'), function()%7B%0A%09%09%09console.log('Express https server listening on port ' + app.get('sslport'));%0A%09%09%7D);%0A%09%7D else %7B%0A%09%09console.log(%22Error: Config file (%22 + configFile + %22) doesn't exist or SSL options are not defined.%22);%0A%09%09process.exit(1);%0A%09%7D%0A%7D%0A%0Aif (useSSL && forcedSSL) %7B%0A%09if (process.env.NODE_ENV == 'heroku') %7B%0A%09%09app.use(function (req, res, next) %7B%0A%09%09%09res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');%0A%09%09%09if (req.headers%5B'x-forwarded-proto'%5D && req.headers%5B'x-forwarded-proto'%5D === %22http%22) %7B%0A%09%09%09%09return res.redirect(301, 'https://' + req.host + req.url);%0A%09%09%09%7D else %7B%0A%09%09%09%09return next();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D else %7B%0A%09%09app.use(function (req, res, next) %7B%0A%09%09res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');%0A%09%09%09if (!req.secure) %7B%0A%09%09%09%09return res.redirect(301, 'https://' + req.hostname  + %22:%22 + app.get('sslport') + req.url);%0A%09%09%09%7D else %7B%0A%09%09%09%09return next();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D;%0A%7D%0A%0Aapp.get('/', routes.index);

Add SSL support