mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
5582d030e3
closes #11999
- When the routes.yaml file changes (manually or through API) we need
to store a checksum to be able to optimize routes reloads in the future
- Added mechanism to detect differences between stored and current routes.yaml hash value
- Added routes.yaml sync on server boot
- Added routes.yaml handling in controllers
- Added routes hash synchronization method in core settings. It lives in core settings
as it needs access to model layer. To avoid coupling with the frontend settings it accepts
a function which has to resolve to a routes hash
- Added note about settings validation side-effect. It mutates input!
- Added async check for currently loaded routes hash
- Extended frontend settings loader with async loader. The default behavior of the loader is
to load settings syncronously for reasons spelled in 0ac19dcf84
To avoid blocking the eventloop added async loading method
- Refactored frontend setting loader for reusability of settings file path
- Added integrity check test for routes.yaml file
105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
const _ = require('lodash');
|
|
const crypto = require('crypto');
|
|
const debug = require('ghost-ignition').debug('frontend:services:settings:index');
|
|
const SettingsLoader = require('./loader');
|
|
const ensureSettingsFiles = require('./ensure-settings');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
/**
|
|
* md5 hashes of default settings
|
|
*/
|
|
const defaultHashes = {
|
|
routes: '3d180d52c663d173a6be791ef411ed01'
|
|
};
|
|
|
|
const calculateHash = (data) => {
|
|
return crypto.createHash('md5')
|
|
.update(data, 'binary')
|
|
.digest('hex');
|
|
};
|
|
|
|
module.exports = {
|
|
init: function () {
|
|
const knownSettings = this.knownSettings();
|
|
|
|
debug('init settings service for:', knownSettings);
|
|
|
|
// Make sure that supported settings files are available
|
|
// inside of the `content/setting` directory
|
|
return ensureSettingsFiles(knownSettings);
|
|
},
|
|
|
|
/**
|
|
* Global place to switch on more available settings.
|
|
*/
|
|
knownSettings: function knownSettings() {
|
|
return ['routes'];
|
|
},
|
|
|
|
/**
|
|
* Getter for YAML settings.
|
|
* Example: `settings.get('routes').then(...)`
|
|
* will return an Object like this:
|
|
* {routes: {}, collections: {}, resources: {}}
|
|
* @param {String} setting type of supported setting.
|
|
* @returns {Object} settingsFile
|
|
* @description Returns settings object as defined per YAML files in
|
|
* `/content/settings` directory.
|
|
*/
|
|
get: function get(setting) {
|
|
const knownSettings = this.knownSettings();
|
|
|
|
// CASE: this should be an edge case and only if internal usage of the
|
|
// getter is incorrect.
|
|
if (!setting || _.indexOf(knownSettings, setting) < 0) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: `Requested setting is not supported: '${setting}'.`,
|
|
help: `Please use only the supported settings: ${knownSettings}.`
|
|
});
|
|
}
|
|
|
|
return SettingsLoader(setting);
|
|
},
|
|
|
|
/**
|
|
* Getter for all YAML settings.
|
|
* Example: `settings.getAll().then(...)`
|
|
* will return an Object like this (assuming we're supporting `routes`
|
|
* and `globals`):
|
|
* {
|
|
* routes: {
|
|
* routes: null,
|
|
* collections: { '/': [Object] },
|
|
* resources: { tag: '/tag/{slug}/', author: '/author/{slug}/' }
|
|
* },
|
|
* globals: {
|
|
* config: { url: 'testblog.com' }
|
|
* }
|
|
* }
|
|
* @returns {Object} settingsObject
|
|
* @description Returns all settings object as defined per YAML files in
|
|
* `/content/settings` directory.
|
|
*/
|
|
getAll: function getAll() {
|
|
const knownSettings = this.knownSettings();
|
|
const settingsToReturn = {};
|
|
|
|
_.each(knownSettings, function (setting) {
|
|
settingsToReturn[setting] = SettingsLoader(setting);
|
|
});
|
|
|
|
return settingsToReturn;
|
|
},
|
|
|
|
getDefaulHash: (setting) => {
|
|
return defaultHashes[setting];
|
|
},
|
|
|
|
getCurrentHash: async (setting) => {
|
|
const data = await SettingsLoader.loadSettings(setting);
|
|
|
|
return calculateHash(JSON.stringify(data));
|
|
}
|
|
};
|