Ghost/core/server/services/settings/index.js
Nazar Gargol 5582d030e3 Added routes.yaml content checksum storage to the db
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
2020-09-10 10:54:57 +12:00

41 lines
1.3 KiB
JavaScript

/**
* Settings Lib
* A collection of utilities for handling settings including a cache
*/
const models = require('../../models');
const SettingsCache = require('./cache');
module.exports = {
async init() {
const settingsCollection = await models.Settings.populateDefaults();
SettingsCache.init(settingsCollection);
},
async reinit() {
SettingsCache.shutdown();
const settingsCollection = await models.Settings.populateDefaults();
SettingsCache.init(settingsCollection);
for (const model of settingsCollection.models) {
model.emitChange(model.attributes.key + '.' + 'edited', {});
}
},
/**
* Handles syncronization of routes.yaml hash loaded in the frontend with
* the value stored in the settings table.
* getRoutesHash is a function to allow keeping "frontend" decoupled from settings
*
* @param {function} getRoutesHash function fetching currently loaded routes file hash
*/
async syncRoutesHash(getRoutesHash) {
const currentRoutesHash = await getRoutesHash();
if (SettingsCache.get('routes_hash') !== currentRoutesHash) {
return await models.Settings.edit([{
key: 'routes_hash',
value: currentRoutesHash
}], {context: {internal: true}});
}
}
};