const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const database = require( './db' );
const fs = require( 'fs' );
const rotator = require('file-stream-rotator')
// --------------------------------------------------------------------------------------
// call express
var app = express();

// connect to the database
database.connect();

// --------------------------------------------------------------------------------------
// view engine setup
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');

// --------------------------------------------------------------------------------------
// set up webserver log files

var log_dir = '/home/etlog/logs/access/';

// ensure log directory exists
fs.existsSync(log_dir) || fs.mkdirSync(log_dir);

// create a rotating write stream
var access_log = rotator.getStream({
  date_format: 'YYYY-MM-DD',
  filename: path.join(log_dir, 'access-%DATE%.log'),
  frequency: 'daily',
  verbose: false
})

// --------------------------------------------------------------------------------------
// uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/favicon.ico'));
// Standard Apache combined log output with added response time and status
// output to access log
app.use(logger(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :response-time[3] ms :status', { stream : access_log }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const saml = require('./auth')(app, database);          // SAML + IP based auth

// --------------------------------------------------------------------------------------
// Make our db accessible to our router
app.use(function(req, res, next){
  req.db = database;
  next();
});

// --------------------------------------------------------------------------------------

// set up routes
require('./routes')(app, database);

// --------------------------------------------------------------------------------------
// set up cron tasks
require('./cron')(database);

// --------------------------------------------------------------------------------------
// set up middleware error handling
require('./error_handling')(app);

// --------------------------------------------------------------------------------------
module.exports = app;

@@ -25,24 +25,122 @@
 'express');%0A
+const session = require('express-session');%0Aconst MongoStore = require('connect-mongo')(session);%0A
 const path =
@@ -153,24 +153,24 @@
 re('path');%0A
-
 const favico
@@ -434,16 +434,60 @@
 tator')%0A
+const secrets = require('./config/secrets')%0A
 // -----
@@ -633,16 +633,16 @@
 atabase%0A
-
 database
@@ -650,24 +650,383 @@
 connect();%0A%0A
+// init session%0Aapp.set('trust proxy', 1)       // app is behind a proxy%0Aapp.use(session(%7B secret : secrets.session,%0A                  secure : true,%0A                  resave : false,%0A                  saveUninitialized : false,%0A                  store : new MongoStore(%7B db : 'SessionStore', url : 'mongodb://localhost:27017/etlog' %7D),%0A                %7D));%0A%0A
 // ---------

use sessions