2014-03-02 14:45:57 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2014-06-25 18:55:22 -04:00
|
|
|
var utils = require('./../../public/src/utils'),
|
2014-03-01 17:26:26 -05:00
|
|
|
meta = require('./../meta'),
|
2014-03-06 12:06:19 -05:00
|
|
|
plugins = require('./../plugins'),
|
2014-03-01 17:26:26 -05:00
|
|
|
db = require('./../database'),
|
|
|
|
|
auth = require('./../routes/authentication'),
|
2014-03-17 12:53:31 -04:00
|
|
|
emitter = require('./../emitter'),
|
|
|
|
|
|
2014-03-01 17:26:26 -05:00
|
|
|
async = require('async'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
nconf = require('nconf'),
|
2014-03-02 14:45:57 -05:00
|
|
|
express = require('express'),
|
2014-04-15 02:16:03 -04:00
|
|
|
winston = require('winston'),
|
2014-06-18 10:17:50 -04:00
|
|
|
flash = require('connect-flash'),
|
2014-06-25 18:55:22 -04:00
|
|
|
templates = require('templates.js'),
|
2014-07-02 14:07:08 -04:00
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
|
cookieParser = require('cookie-parser'),
|
|
|
|
|
compression = require('compression'),
|
|
|
|
|
favicon = require('serve-favicon'),
|
2014-07-02 14:59:48 -04:00
|
|
|
multipart = require('connect-multiparty'),
|
2014-07-02 14:07:08 -04:00
|
|
|
session = require('express-session'),
|
2014-09-03 01:24:26 -04:00
|
|
|
cluster = require('cluster'),
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-04-15 02:16:03 -04:00
|
|
|
relativePath,
|
2014-08-26 14:48:05 -04:00
|
|
|
themesPath;
|
2014-03-01 17:26:26 -05:00
|
|
|
|
|
|
|
|
|
2014-04-15 02:16:03 -04:00
|
|
|
var middleware = {};
|
2014-03-05 17:06:24 -05:00
|
|
|
|
2014-05-01 15:06:20 -04:00
|
|
|
function routeCurrentTheme(app, themeId, themesData) {
|
2014-10-08 12:22:39 -04:00
|
|
|
themeId = (themeId || 'nodebb-theme-vanilla');
|
|
|
|
|
|
|
|
|
|
var themeObj = (function(id) {
|
2014-05-01 15:06:20 -04:00
|
|
|
return themesData.filter(function(themeObj) {
|
|
|
|
|
return themeObj.id === id;
|
|
|
|
|
})[0];
|
|
|
|
|
})(themeId);
|
2014-03-01 17:26:26 -05:00
|
|
|
|
|
|
|
|
// Detect if a theme has been selected, and handle appropriately
|
2014-04-02 14:41:52 -04:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
winston.info('[themes] Using theme ' + themeId);
|
|
|
|
|
}
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-09-29 19:04:28 -04:00
|
|
|
meta.themes.setPath(themeObj);
|
2014-03-05 15:13:54 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 19:37:09 -04:00
|
|
|
function setupFavicon() {
|
|
|
|
|
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
|
|
|
|
|
if (fs.existsSync(faviconPath)) {
|
|
|
|
|
app.use(favicon(faviconPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 17:26:26 -05:00
|
|
|
module.exports = function(app, data) {
|
2014-03-02 14:16:16 -05:00
|
|
|
middleware = require('./middleware')(app);
|
|
|
|
|
|
2014-04-15 02:16:03 -04:00
|
|
|
relativePath = nconf.get('relative_path');
|
|
|
|
|
themesPath = nconf.get('themes_path');
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.engine('tpl', templates.__express);
|
|
|
|
|
app.set('view engine', 'tpl');
|
2014-08-26 14:48:05 -04:00
|
|
|
app.set('views', nconf.get('views_dir'));
|
2014-07-02 14:59:48 -04:00
|
|
|
app.set('json spaces', process.env.NODE_ENV === 'development' ? 4 : 0);
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(flash());
|
2014-05-05 16:56:01 -04:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.enable('view cache');
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(compression());
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-10-17 19:37:09 -04:00
|
|
|
setupFavicon();
|
|
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(relativePath + '/apple-touch-icon', middleware.routeTouchIcon);
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:59:48 -04:00
|
|
|
app.use(bodyParser.urlencoded({extended: true}));
|
|
|
|
|
app.use(bodyParser.json());
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(cookieParser());
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-24 22:26:19 +02:00
|
|
|
var cookie = {
|
2014-09-29 19:29:13 -04:00
|
|
|
maxAge: 1000 * 60 * 60 * 24 * parseInt(meta.config.loginDays || 14, 10)
|
2014-07-24 22:26:19 +02:00
|
|
|
};
|
2014-07-25 00:43:15 +02:00
|
|
|
if(meta.config.cookieDomain) {
|
|
|
|
|
cookie.domain = meta.config.cookieDomain;
|
2014-07-24 22:26:19 +02:00
|
|
|
}
|
2014-08-13 16:03:33 -04:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(session({
|
|
|
|
|
store: db.sessionStore,
|
|
|
|
|
secret: nconf.get('secret'),
|
|
|
|
|
key: 'express.sid',
|
2014-07-24 22:26:19 +02:00
|
|
|
cookie: cookie,
|
2014-07-02 14:59:48 -04:00
|
|
|
resave: true,
|
|
|
|
|
saveUninitialized: true
|
2014-07-02 14:07:08 -04:00
|
|
|
}));
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:59:48 -04:00
|
|
|
app.use(multipart());
|
2014-06-02 16:52:16 -04:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
|
res.setHeader('X-Powered-By', 'NodeBB');
|
2014-06-02 16:52:16 -04:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
|
|
|
if (meta.config['allow-from-uri']) {
|
|
|
|
|
res.setHeader('ALLOW-FROM', meta.config['allow-from-uri']);
|
|
|
|
|
}
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
next();
|
|
|
|
|
});
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
app.use(middleware.processRender);
|
2014-03-01 17:26:26 -05:00
|
|
|
|
2014-09-21 14:29:27 -04:00
|
|
|
auth.initialize(app, middleware);
|
2014-03-06 12:06:19 -05:00
|
|
|
|
2014-07-02 14:07:08 -04:00
|
|
|
routeCurrentTheme(app, data.currentThemeId, data.themesData);
|
2014-09-04 17:09:57 -04:00
|
|
|
meta.templates.compile();
|
2014-03-02 14:16:16 -05:00
|
|
|
|
|
|
|
|
return middleware;
|
2014-04-10 20:31:57 +01:00
|
|
|
};
|