2015-07-15 18:01:23 +02:00
|
|
|
var bodyParser = require('body-parser'),
|
|
|
|
config = require('../config'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
express = require('express'),
|
|
|
|
logger = require('morgan'),
|
|
|
|
path = require('path'),
|
|
|
|
routes = require('../routes'),
|
|
|
|
slashes = require('connect-slashes'),
|
|
|
|
storage = require('../storage'),
|
|
|
|
passport = require('passport'),
|
|
|
|
oauth2orize = require('oauth2orize'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
sitemapHandler = require('../data/xml/sitemap/handler'),
|
|
|
|
|
|
|
|
authStrategies = require('./auth-strategies'),
|
|
|
|
busboy = require('./ghost-busboy'),
|
2015-10-22 15:28:47 +02:00
|
|
|
auth = require('./auth'),
|
2015-07-15 18:01:23 +02:00
|
|
|
cacheControl = require('./cache-control'),
|
|
|
|
checkSSL = require('./check-ssl'),
|
|
|
|
decideIsAdmin = require('./decide-is-admin'),
|
2015-10-11 21:21:54 +01:00
|
|
|
oauth = require('./oauth'),
|
2015-07-15 18:01:23 +02:00
|
|
|
privateBlogging = require('./private-blogging'),
|
|
|
|
redirectToSetup = require('./redirect-to-setup'),
|
|
|
|
serveSharedFile = require('./serve-shared-file'),
|
|
|
|
spamPrevention = require('./spam-prevention'),
|
|
|
|
staticTheme = require('./static-theme'),
|
|
|
|
themeHandler = require('./theme-handler'),
|
2015-10-11 21:21:54 +01:00
|
|
|
uncapitalise = require('./uncapitalise'),
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2015-08-21 23:46:42 +02:00
|
|
|
ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy,
|
|
|
|
BearerStrategy = require('passport-http-bearer').Strategy,
|
|
|
|
|
2015-07-15 18:01:23 +02:00
|
|
|
middleware,
|
2014-07-28 15:19:49 +02:00
|
|
|
setupMiddleware;
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2015-07-15 18:01:23 +02:00
|
|
|
middleware = {
|
|
|
|
busboy: busboy,
|
|
|
|
cacheControl: cacheControl,
|
|
|
|
spamPrevention: spamPrevention,
|
|
|
|
privateBlogging: privateBlogging,
|
|
|
|
api: {
|
2015-10-22 15:28:47 +02:00
|
|
|
cacheOauthServer: auth.cacheOauthServer,
|
|
|
|
authenticateClient: auth.authenticateClient,
|
|
|
|
authenticateUser: auth.authenticateUser,
|
|
|
|
requiresAuthorizedUser: auth.requiresAuthorizedUser,
|
2015-10-23 11:03:38 +02:00
|
|
|
requiresAuthorizedUserPublicAPI: auth.requiresAuthorizedUserPublicAPI,
|
2015-10-22 15:28:47 +02:00
|
|
|
generateAccessToken: auth.generateAccessToken,
|
|
|
|
errorHandler: errors.handleAPIError
|
2015-04-25 19:54:07 +00:00
|
|
|
}
|
2015-07-15 18:01:23 +02:00
|
|
|
};
|
2014-03-09 09:28:58 +00:00
|
|
|
|
2015-10-11 21:21:54 +01:00
|
|
|
setupMiddleware = function setupMiddleware(blogApp, adminApp) {
|
2014-07-17 10:33:21 -04:00
|
|
|
var logging = config.logging,
|
|
|
|
corePath = config.paths.corePath,
|
2014-06-30 14:58:10 +02:00
|
|
|
oauthServer = oauth2orize.createServer();
|
|
|
|
|
|
|
|
// silence JSHint without disabling unused check for the whole file
|
2015-08-21 23:46:42 +02:00
|
|
|
passport.use(new ClientPasswordStrategy(authStrategies.clientPasswordStrategy));
|
|
|
|
passport.use(new BearerStrategy(authStrategies.bearerStrategy));
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2013-12-06 09:13:15 -05:00
|
|
|
// Cache express server instance
|
2015-06-14 20:07:52 +01:00
|
|
|
middleware.api.cacheOauthServer(oauthServer);
|
2015-07-15 18:01:23 +02:00
|
|
|
oauth.init(oauthServer, spamPrevention.resetCounter);
|
2013-12-06 09:13:15 -05:00
|
|
|
|
2014-01-26 17:00:50 -05:00
|
|
|
// Make sure 'req.secure' is valid for proxied requests
|
|
|
|
// (X-Forwarded-Proto header will be checked, if present)
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.enable('trust proxy');
|
2014-01-26 17:00:50 -05:00
|
|
|
|
2013-11-12 01:03:25 -05:00
|
|
|
// Logging configuration
|
2014-02-10 21:07:11 +00:00
|
|
|
if (logging !== false) {
|
2014-09-19 16:17:58 +00:00
|
|
|
if (blogApp.get('env') !== 'development') {
|
|
|
|
blogApp.use(logger('combined', logging));
|
2014-02-10 21:07:11 +00:00
|
|
|
} else {
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(logger('dev', logging));
|
2014-02-10 21:07:11 +00:00
|
|
|
}
|
2013-11-12 01:03:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Favicon
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(serveSharedFile('favicon.ico', 'image/x-icon', utils.ONE_DAY_S));
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2013-12-30 23:13:25 +00:00
|
|
|
// Static assets
|
2015-10-12 17:54:15 +01:00
|
|
|
blogApp.use('/shared', express.static(path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS}));
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use('/content/images', storage.getStorage().serve());
|
2015-10-12 17:54:15 +01:00
|
|
|
blogApp.use('/public', express.static(path.join(corePath, '/built/public'), {maxAge: utils.ONE_YEAR_MS}));
|
2013-11-12 01:03:25 -05:00
|
|
|
|
|
|
|
// First determine whether we're serving admin or theme content
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(decideIsAdmin);
|
2015-10-11 21:21:54 +01:00
|
|
|
blogApp.use(themeHandler.updateActiveTheme);
|
|
|
|
blogApp.use(themeHandler.configHbsForContext);
|
2013-11-12 01:03:25 -05:00
|
|
|
|
|
|
|
// Admin only config
|
2015-10-12 17:54:15 +01:00
|
|
|
blogApp.use('/ghost', express.static(config.paths.clientAssets, {maxAge: utils.ONE_YEAR_MS}));
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2014-01-26 17:21:24 -05:00
|
|
|
// Force SSL
|
|
|
|
// NOTE: Importantly this is _after_ the check above for admin-theme static resources,
|
|
|
|
// which do not need HTTPS. In fact, if HTTPS is forced on them, then 404 page might
|
|
|
|
// not display properly when HTTPS is not available!
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.use(checkSSL);
|
2014-09-19 16:17:58 +00:00
|
|
|
adminApp.set('views', config.paths.adminViews);
|
2014-01-26 17:21:24 -05:00
|
|
|
|
2013-11-12 01:03:25 -05:00
|
|
|
// Theme only config
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.use(staticTheme());
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2015-03-26 02:01:39 -05:00
|
|
|
// Check if password protected blog
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.use(privateBlogging.checkIsPrivate); // check if the blog is protected
|
|
|
|
blogApp.use(privateBlogging.filterPrivateRoutes);
|
2015-03-26 02:01:39 -05:00
|
|
|
|
|
|
|
// Serve sitemap.xsl file
|
|
|
|
blogApp.use(serveSharedFile('sitemap.xsl', 'text/xsl', utils.ONE_DAY_S));
|
|
|
|
|
2014-03-09 09:28:58 +00:00
|
|
|
// Serve robots.txt if not found in theme
|
2014-09-25 10:00:51 +00:00
|
|
|
blogApp.use(serveSharedFile('robots.txt', 'text/plain', utils.ONE_HOUR_S));
|
2014-03-09 09:28:58 +00:00
|
|
|
|
2014-10-27 19:41:18 -05:00
|
|
|
// site map
|
|
|
|
sitemapHandler(blogApp);
|
|
|
|
|
2014-12-25 00:22:45 +00:00
|
|
|
// Add in all trailing slashes
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(slashes(true, {
|
2014-08-23 16:42:44 -04:00
|
|
|
headers: {
|
|
|
|
'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S
|
2014-12-25 00:22:45 +00:00
|
|
|
}
|
2014-08-23 16:42:44 -04:00
|
|
|
}));
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(uncapitalise);
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2013-12-30 23:13:25 +00:00
|
|
|
// Body parsing
|
2015-10-30 13:40:07 +01:00
|
|
|
blogApp.use(bodyParser.json({limit: '1mb'}));
|
|
|
|
blogApp.use(bodyParser.urlencoded({extended: true, limit: '1mb'}));
|
2013-11-17 18:40:26 +00:00
|
|
|
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(passport.initialize());
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2014-02-14 11:00:11 +01:00
|
|
|
// ### Caching
|
2015-05-18 20:56:41 +01:00
|
|
|
// Blog frontend is cacheable
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.use(cacheControl('public'));
|
2015-05-18 20:56:41 +01:00
|
|
|
// Admin shouldn't be cached
|
2015-07-15 18:01:23 +02:00
|
|
|
adminApp.use(cacheControl('private'));
|
2015-05-18 20:56:41 +01:00
|
|
|
// API shouldn't be cached
|
2015-07-15 18:01:23 +02:00
|
|
|
blogApp.use(routes.apiBaseUri, cacheControl('private'));
|
2014-02-14 11:00:11 +01:00
|
|
|
|
2013-11-12 01:03:25 -05:00
|
|
|
// local data
|
2015-10-11 21:21:54 +01:00
|
|
|
blogApp.use(themeHandler.ghostLocals);
|
2014-04-28 22:58:18 +02:00
|
|
|
|
2013-12-30 23:13:25 +00:00
|
|
|
// ### Routing
|
2014-04-11 23:46:15 -04:00
|
|
|
// Set up API routes
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(routes.apiBaseUri, routes.api(middleware));
|
2014-04-11 23:46:15 -04:00
|
|
|
|
2014-09-16 21:02:31 +00:00
|
|
|
// Mount admin express app to /ghost and set up routes
|
2015-07-15 18:01:23 +02:00
|
|
|
adminApp.use(redirectToSetup);
|
2014-09-19 16:17:58 +00:00
|
|
|
adminApp.use(routes.admin());
|
|
|
|
blogApp.use('/ghost', adminApp);
|
2014-04-11 23:46:15 -04:00
|
|
|
|
|
|
|
// Set up Frontend routes
|
2015-03-26 02:01:39 -05:00
|
|
|
blogApp.use(routes.frontend(middleware));
|
2013-11-12 01:03:25 -05:00
|
|
|
|
|
|
|
// ### Error handling
|
|
|
|
// 404 Handler
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(errors.error404);
|
2013-11-12 01:03:25 -05:00
|
|
|
|
|
|
|
// 500 Handler
|
2014-09-19 16:17:58 +00:00
|
|
|
blogApp.use(errors.error500);
|
2013-11-12 01:03:25 -05:00
|
|
|
};
|
|
|
|
|
2014-07-14 13:29:45 +01:00
|
|
|
module.exports = setupMiddleware;
|
2013-11-12 01:03:25 -05:00
|
|
|
// Export middleware functions directly
|
2013-11-17 18:40:26 +00:00
|
|
|
module.exports.middleware = middleware;
|