🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 18:24:09 +03:00
|
|
|
var debug = require('debug')('ghost:blog'),
|
|
|
|
path = require('path'),
|
|
|
|
|
|
|
|
// App requires
|
|
|
|
config = require('../config'),
|
|
|
|
storage = require('../storage'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
|
|
|
|
// This should probably be an internal app
|
|
|
|
sitemapHandler = require('../data/xml/sitemap/handler'),
|
|
|
|
|
|
|
|
// routes
|
|
|
|
routes = require('./routes'),
|
|
|
|
|
|
|
|
// local middleware
|
|
|
|
cacheControl = require('../middleware/cache-control'),
|
|
|
|
checkSSL = require('../middleware/check-ssl'),
|
|
|
|
errorHandler = require('../middleware/error-handler'),
|
|
|
|
maintenance = require('../middleware/maintenance'),
|
|
|
|
prettyURLs = require('../middleware/pretty-urls'),
|
|
|
|
serveSharedFile = require('../middleware/serve-shared-file'),
|
|
|
|
staticTheme = require('../middleware/static-theme'),
|
2017-01-26 21:01:19 +03:00
|
|
|
themeHandler = require('../middleware/theme-handler'),
|
|
|
|
serveFavicon = require('../middleware/serve-favicon');
|
🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 18:24:09 +03:00
|
|
|
|
|
|
|
module.exports = function setupBlogApp() {
|
|
|
|
debug('Blog setup start');
|
|
|
|
|
|
|
|
var blogApp = require('express')();
|
|
|
|
|
|
|
|
// ## App - specific code
|
|
|
|
// set the view engine
|
|
|
|
blogApp.set('view engine', 'hbs');
|
|
|
|
|
|
|
|
// Theme middleware
|
|
|
|
// rightly or wrongly currently comes before theme static assets
|
|
|
|
// @TODO revisit where and when these are needed
|
|
|
|
blogApp.use(themeHandler.updateActiveTheme);
|
|
|
|
blogApp.use(themeHandler.configHbsForContext);
|
|
|
|
debug('Themes done');
|
|
|
|
|
|
|
|
// Static content/assets
|
|
|
|
// @TODO make sure all of these have a local 404 error handler
|
|
|
|
// Favicon
|
2017-01-26 21:01:19 +03:00
|
|
|
blogApp.use(serveFavicon());
|
🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 18:24:09 +03:00
|
|
|
// Ghost-Url
|
|
|
|
blogApp.use(serveSharedFile('shared/ghost-url.js', 'application/javascript', utils.ONE_HOUR_S));
|
|
|
|
blogApp.use(serveSharedFile('shared/ghost-url.min.js', 'application/javascript', utils.ONE_HOUR_S));
|
|
|
|
// Serve sitemap.xsl file
|
|
|
|
blogApp.use(serveSharedFile('sitemap.xsl', 'text/xsl', utils.ONE_DAY_S));
|
|
|
|
// Serve robots.txt if not found in theme
|
|
|
|
blogApp.use(serveSharedFile('robots.txt', 'text/plain', utils.ONE_HOUR_S));
|
|
|
|
// Serve blog images using the storage adapter
|
|
|
|
blogApp.use('/content/images', storage.getStorage().serve());
|
|
|
|
|
|
|
|
// Theme static assets/files
|
|
|
|
blogApp.use(staticTheme());
|
|
|
|
debug('Static content done');
|
|
|
|
|
|
|
|
// setup middleware for internal apps
|
|
|
|
// @TODO: refactor this to be a proper app middleware hook for internal & external apps
|
|
|
|
config.get('internalApps').forEach(function (appName) {
|
|
|
|
var app = require(path.join(config.get('paths').internalAppPath, appName));
|
|
|
|
if (app.hasOwnProperty('setupMiddleware')) {
|
|
|
|
app.setupMiddleware(blogApp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// site map - this should probably be refactored to be an internal app
|
|
|
|
sitemapHandler(blogApp);
|
|
|
|
debug('Internal apps done');
|
|
|
|
|
|
|
|
// send 503 error page in case of maintenance
|
|
|
|
blogApp.use(maintenance);
|
|
|
|
|
|
|
|
// Force SSL if required
|
|
|
|
// must happen AFTER asset loading and BEFORE routing
|
|
|
|
blogApp.use(checkSSL);
|
|
|
|
|
|
|
|
// Add in all trailing slashes & remove uppercase
|
|
|
|
// must happen AFTER asset loading and BEFORE routing
|
|
|
|
blogApp.use(prettyURLs);
|
|
|
|
|
|
|
|
// ### Caching
|
|
|
|
// Blog frontend is cacheable
|
|
|
|
blogApp.use(cacheControl('public'));
|
|
|
|
|
|
|
|
debug('General middleware done');
|
|
|
|
|
|
|
|
// Set up Frontend routes (including private blogging routes)
|
|
|
|
blogApp.use(routes());
|
|
|
|
|
|
|
|
// ### Error handlers
|
|
|
|
blogApp.use(errorHandler.pageNotFound);
|
|
|
|
blogApp.use(errorHandler.handleHTMLResponse);
|
|
|
|
|
|
|
|
debug('Blog setup end');
|
|
|
|
|
|
|
|
return blogApp;
|
|
|
|
};
|