"use strict";

var express = require('express');
var config = require('config');
var hosting_app = require('./hosting-app/app');
var instance_app = require('./instance-app/app');

var app = module.exports = express();

app.enable('trust proxy');

// Intercept JSON content type GET requests and change the header.
// this is a workaround for https://github.com/senchalabs/connect/issues/680 which hopefully will be fixed upstream
app.use( function (req, res, next) {
  if ( (req.method == "GET" || req.method == "DELETE" ) && req.headers['content-type'] == 'application/json' ) {
    delete req.headers['content-type'];
  }
  next();
});

if (app.get('env') === 'development') {
  app.use(express.logger({ format: 'dev' }));
}

if (app.get('env') === 'production') {
  app.use(express.logger());
}

if (config.force_https) {
  app.use(require('./lib/middleware/secure'));
}

app.use(express.static(__dirname + '/' + config.public_dir));

// sessions
app.use(express.cookieParser());

app.use(express.cookieSession({
  secret: config.instance_server.cookie_secret,
  cookie: { domain: config.instance_server.cookie_domain },
}));

// set up the flash and make it available to the templates - https://gist.github.com/3070950
app.use(require('connect-flash')());
app.use(function(req, res, next) {
  res.locals.flash = req.flash.bind(req);
  next();
});

app.use(require('./lib/middleware/route'));

// match the hosting app host...
app.use(
  express.vhost(
    config.hosting_server.host,
    hosting_app
  )
);

// ...or fall through to the instance app
app.use(instance_app);

"use strict";

var express = require('express');
var config = require('config');
var hosting_app = require('./hosting-app/app');
var instance_app = require('./instance-app/app');

var app = module.exports = express();

app.enable('trust proxy');

// Intercept JSON content type GET requests and change the header.
// this is a workaround for https://github.com/senchalabs/connect/issues/680 which hopefully will be fixed upstream
app.use( function (req, res, next) {
  if ( (req.method == "GET" || req.method == "DELETE" ) && req.headers['content-type'] == 'application/json' ) {
    delete req.headers['content-type'];
  }
  next();
});

if (app.get('env') === 'development') {
  app.use(express.logger({ format: 'dev' }));
}

if (app.get('env') === 'production') {
  app.use(express.logger());
}

if (config.force_https) {
  app.use(require('./lib/middleware/secure'));
}

app.use(express.static(__dirname + '/' + config.public_dir));

// sessions
app.use(express.cookieParser());

app.use(express.cookieSession({
  secret: config.instance_server.cookie_secret,
  cookie: { domain: config.instance_server.cookie_domain, secure: config.force_https },
  proxy: true,
}));

// set up the flash and make it available to the templates - https://gist.github.com/3070950
app.use(require('connect-flash')());
app.use(function(req, res, next) {
  res.locals.flash = req.flash.bind(req);
  next();
});

app.use(require('./lib/middleware/route'));

// match the hosting app host...
app.use(
  express.vhost(
    config.hosting_server.host,
    hosting_app
  )
);

// ...or fall through to the instance app
app.use(instance_app);

Enable secure cookies