Ghost/core/frontend/apps/private-blogging/index.js
Hannah Wolfe 9d7049cd3f
Refactored helper registration code into a service
- The helper registration code is "framework" code and very specific
- At the moment the "theme engine" is full of lots of disparate theme related stuff
- I'm trying to make the frontend framework code clearer and also expand it to  make it more useful
- The helper system now also exposes 3 methods allowing you to register a directory, a helper or an alias
- I've updated the codebase to use these both for our core helpers and for "apps"
2021-10-05 10:04:02 +01:00

58 lines
1.7 KiB
JavaScript

const path = require('path');
const tpl = require('@tryghost/tpl');
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
const urlUtils = require('../../../shared/url-utils');
const middleware = require('./lib/middleware');
const router = require('./lib/router');
const messages = {
urlCannotContainPrivateSubdir: {
error: 'private subdirectory not allowed',
description: 'Your site url in config.js cannot contain a subdirectory called private.',
help: 'Please rename the subdirectory before restarting'
}
};
// routeKeywords.private: 'private'
const PRIVATE_KEYWORD = 'private';
let checkSubdir = function checkSubdir() {
let paths = '';
if (urlUtils.getSubdir()) {
paths = urlUtils.getSubdir().split('/');
if (paths.pop() === PRIVATE_KEYWORD) {
logging.error(new errors.GhostError({
message: tpl(messages.urlCannotContainPrivateSubdir.error),
context: tpl(messages.urlCannotContainPrivateSubdir.description),
help: tpl(messages.urlCannotContainPrivateSubdir.help)
}));
// @TODO: why
process.exit(0);
}
}
};
module.exports = {
activate: function activate(ghost) {
let privateRoute = `/${PRIVATE_KEYWORD}/`;
checkSubdir();
ghost.routeService.registerRouter(privateRoute, router);
ghost.helperService.registerDir(path.resolve(__dirname, './lib/helpers'));
},
setupMiddleware: function setupMiddleware(siteApp) {
siteApp.use(middleware.checkIsPrivate);
siteApp.use(middleware.filterPrivateRoutes);
},
setupErrorHandling: function setupErrorHandling(siteApp) {
siteApp.use(middleware.handle404);
}
};