mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Merge pull request #3867 from hswolff/middleware-work
Makes the Ghost application more express middleware friendly.
This commit is contained in:
commit
9c0b203dce
4 changed files with 50 additions and 27 deletions
|
@ -9,6 +9,9 @@ function GhostServer(app) {
|
|||
this.httpServer = null;
|
||||
this.connections = [];
|
||||
this.upgradeWarning = setTimeout(this.logUpgradeWarning.bind(this), 5000);
|
||||
|
||||
// Expose config module for use externally.
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
GhostServer.prototype.connection = function (socket) {
|
||||
|
@ -86,9 +89,16 @@ GhostServer.prototype.logUpgradeWarning = function () {
|
|||
console.log('Warning: Ghost will no longer start automatically when using it as an npm module. Please see the docs(http://tinyurl.com/npm-upgrade) for information on how to update your code.'.red);
|
||||
};
|
||||
|
||||
// Starts the ghost server listening on the configured port
|
||||
GhostServer.prototype.start = function () {
|
||||
var self = this;
|
||||
/**
|
||||
* Starts the ghost server listening on the configured port.
|
||||
* Alternatively you can pass in your own express instance and let Ghost
|
||||
* start lisetning for you.
|
||||
* @param {Object=} externalApp Optional express app instance.
|
||||
* @return {Promise}
|
||||
*/
|
||||
GhostServer.prototype.start = function (externalApp) {
|
||||
var self = this,
|
||||
app = externalApp ? externalApp : self.app;
|
||||
|
||||
// ## Start Ghost App
|
||||
return new Promise(function (resolve) {
|
||||
|
@ -100,14 +110,14 @@ GhostServer.prototype.start = function () {
|
|||
// We can ignore this.
|
||||
}
|
||||
|
||||
self.httpServer = self.app.listen(
|
||||
self.httpServer = app.listen(
|
||||
config.getSocket()
|
||||
);
|
||||
|
||||
fs.chmod(config.getSocket(), '0660');
|
||||
|
||||
} else {
|
||||
self.httpServer = self.app.listen(
|
||||
self.httpServer = app.listen(
|
||||
config.server.port,
|
||||
config.server.host
|
||||
);
|
||||
|
|
|
@ -37,8 +37,8 @@ function ghostLocals(req, res, next) {
|
|||
// Make sure we have a locals value.
|
||||
res.locals = res.locals || {};
|
||||
res.locals.version = packageInfo.version;
|
||||
// relative path from the URL, not including subdir
|
||||
res.locals.relativeUrl = req.path.replace(config.paths.subdir, '');
|
||||
// relative path from the URL
|
||||
res.locals.relativeUrl = req.path;
|
||||
|
||||
next();
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ function activateTheme(activeTheme) {
|
|||
// Uses the URL to detect whether this response should be an admin response
|
||||
// This is used to ensure the right content is served, and is not for security purposes
|
||||
function decideIsAdmin(req, res, next) {
|
||||
res.isAdmin = req.url.lastIndexOf(config.paths.subdir + '/ghost/', 0) === 0;
|
||||
res.isAdmin = req.url.lastIndexOf('/ghost/', 0) === 0;
|
||||
next();
|
||||
}
|
||||
|
||||
|
@ -248,7 +248,6 @@ function robots() {
|
|||
|
||||
setupMiddleware = function (server) {
|
||||
var logging = config.logging,
|
||||
subdir = config.paths.subdir,
|
||||
corePath = config.paths.corePath,
|
||||
oauthServer = oauth2orize.createServer();
|
||||
|
||||
|
@ -275,13 +274,13 @@ setupMiddleware = function (server) {
|
|||
}
|
||||
|
||||
// Favicon
|
||||
expressServer.use(subdir, favicon(corePath + '/shared/favicon.ico'));
|
||||
expressServer.use(favicon(corePath + '/shared/favicon.ico'));
|
||||
|
||||
// Static assets
|
||||
expressServer.use(subdir + '/shared', express['static'](path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS}));
|
||||
expressServer.use(subdir + '/content/images', storage.get_storage().serve());
|
||||
expressServer.use(subdir + '/ghost/scripts', express['static'](path.join(corePath, '/built/scripts'), {maxAge: utils.ONE_YEAR_MS}));
|
||||
expressServer.use(subdir + '/public', express['static'](path.join(corePath, '/built/public'), {maxAge: utils.ONE_YEAR_MS}));
|
||||
expressServer.use('/shared', express['static'](path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS}));
|
||||
expressServer.use('/content/images', storage.get_storage().serve());
|
||||
expressServer.use('/ghost/scripts', express['static'](path.join(corePath, '/built/scripts'), {maxAge: utils.ONE_YEAR_MS}));
|
||||
expressServer.use('/public', express['static'](path.join(corePath, '/built/public'), {maxAge: utils.ONE_YEAR_MS}));
|
||||
|
||||
// First determine whether we're serving admin or theme content
|
||||
expressServer.use(decideIsAdmin);
|
||||
|
@ -289,7 +288,7 @@ setupMiddleware = function (server) {
|
|||
expressServer.use(configHbsForContext);
|
||||
|
||||
// Admin only config
|
||||
expressServer.use(subdir + '/ghost', middleware.whenEnabled('admin', express['static'](path.join(corePath, '/client/assets'), {maxAge: utils.ONE_YEAR_MS})));
|
||||
expressServer.use('/ghost', middleware.whenEnabled('admin', express['static'](path.join(corePath, '/client/assets'), {maxAge: utils.ONE_YEAR_MS})));
|
||||
|
||||
// Force SSL
|
||||
// NOTE: Importantly this is _after_ the check above for admin-theme static resources,
|
||||
|
@ -298,13 +297,19 @@ setupMiddleware = function (server) {
|
|||
expressServer.use(checkSSL);
|
||||
|
||||
// Theme only config
|
||||
expressServer.use(subdir, middleware.staticTheme());
|
||||
expressServer.use(middleware.staticTheme());
|
||||
|
||||
// Serve robots.txt if not found in theme
|
||||
expressServer.use(robots());
|
||||
|
||||
// Handle trailing slashes and capitalization of routes
|
||||
expressServer.use(slashes(true, {headers: {'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S}}));
|
||||
// Add in all trailing slashes, properly include the subdir path
|
||||
// in the redirect.
|
||||
expressServer.use(slashes(true, {
|
||||
headers: {
|
||||
'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S
|
||||
},
|
||||
base: config.paths.subdir
|
||||
}));
|
||||
expressServer.use(uncapitalise);
|
||||
|
||||
// Body parsing
|
||||
|
@ -315,7 +320,7 @@ setupMiddleware = function (server) {
|
|||
|
||||
// ### Caching
|
||||
expressServer.use(middleware.cacheControl('public'));
|
||||
expressServer.use(subdir + '/ghost/', middleware.cacheControl('private'));
|
||||
expressServer.use('/ghost/', middleware.cacheControl('private'));
|
||||
|
||||
|
||||
// enable authentication
|
||||
|
@ -326,13 +331,13 @@ setupMiddleware = function (server) {
|
|||
|
||||
// ### Routing
|
||||
// Set up API routes
|
||||
expressServer.use(subdir + routes.apiBaseUri, routes.api(middleware));
|
||||
expressServer.use(routes.apiBaseUri, routes.api(middleware));
|
||||
|
||||
// Set up Admin routes
|
||||
expressServer.use(subdir, routes.admin(middleware));
|
||||
expressServer.use(routes.admin(middleware));
|
||||
|
||||
// Set up Frontend routes
|
||||
expressServer.use(subdir, routes.frontend());
|
||||
expressServer.use(routes.frontend());
|
||||
|
||||
// ### Error handling
|
||||
// 404 Handler
|
||||
|
|
|
@ -43,7 +43,7 @@ var middleware = {
|
|||
|
||||
// SubPath is the url path starting after any default subdirectories
|
||||
// it is stripped of anything after the two levels `/ghost/.*?/` as the reset link has an argument
|
||||
path = req.path.substring(config.paths.subdir.length);
|
||||
path = req.path;
|
||||
/*jslint regexp:true, unparam:true*/
|
||||
subPath = path.replace(/^(\/.*?\/.*?\/)(.*)?/, function (match, a) {
|
||||
return a;
|
||||
|
|
16
index.js
16
index.js
|
@ -2,11 +2,19 @@
|
|||
// Orchestrates the loading of Ghost
|
||||
// When run from command line.
|
||||
|
||||
var ghost = require('./core'),
|
||||
errors = require('./core/server/errors');
|
||||
var express = require('express'),
|
||||
ghost = require('./core'),
|
||||
errors = require('./core/server/errors');
|
||||
|
||||
ghost().then(function (app) {
|
||||
app.start();
|
||||
// Create our parent express app instance.
|
||||
var server = express();
|
||||
|
||||
ghost().then(function (instance) {
|
||||
// Mount our ghost instance on our desired subdirectory path if it exists.
|
||||
server.use(instance.config.paths.subdir, instance.app);
|
||||
|
||||
// Let ghost handle starting our server instance.
|
||||
instance.start(server);
|
||||
}).catch(function (err) {
|
||||
errors.logErrorAndExit(err, err.context, err.help);
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue