2020-09-09 15:28:12 +03:00
|
|
|
const crypto = require('crypto');
|
2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('frontend:services:settings:index');
|
2019-06-25 19:33:56 +03:00
|
|
|
const SettingsLoader = require('./loader');
|
2021-09-23 14:21:44 +03:00
|
|
|
const ensureSettingsFile = require('./ensure-settings');
|
2019-06-25 19:33:56 +03:00
|
|
|
|
2020-09-09 15:28:12 +03:00
|
|
|
/**
|
2021-09-23 15:27:58 +03:00
|
|
|
* md5 hashes of default routes settings
|
2020-09-09 15:28:12 +03:00
|
|
|
*/
|
2021-09-23 15:27:58 +03:00
|
|
|
const defaultRoutesSettingHash = '3d180d52c663d173a6be791ef411ed01';
|
2020-09-09 15:28:12 +03:00
|
|
|
|
|
|
|
const calculateHash = (data) => {
|
|
|
|
return crypto.createHash('md5')
|
|
|
|
.update(data, 'binary')
|
|
|
|
.digest('hex');
|
|
|
|
};
|
|
|
|
|
2019-06-25 19:33:56 +03:00
|
|
|
module.exports = {
|
|
|
|
init: function () {
|
2021-09-23 14:21:44 +03:00
|
|
|
debug('init routes settings service');
|
2019-06-25 19:33:56 +03:00
|
|
|
|
|
|
|
// Make sure that supported settings files are available
|
|
|
|
// inside of the `content/setting` directory
|
2021-09-23 14:21:44 +03:00
|
|
|
return ensureSettingsFile('routes.yaml');
|
2019-06-25 19:33:56 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2021-09-23 15:23:11 +03:00
|
|
|
* Getter for routes YAML setting.
|
|
|
|
* Example: `settings.get().then(...)`
|
|
|
|
* will return a JSON Object like this:
|
2019-06-25 19:33:56 +03:00
|
|
|
* {routes: {}, collections: {}, resources: {}}
|
2021-09-23 15:23:11 +03:00
|
|
|
* @returns {Object} routes.yaml in JSON format
|
2019-06-25 19:33:56 +03:00
|
|
|
*/
|
2021-09-23 15:23:11 +03:00
|
|
|
get: function get() {
|
|
|
|
return SettingsLoader('routes');
|
2019-06-25 19:33:56 +03:00
|
|
|
},
|
|
|
|
|
2021-09-23 15:27:58 +03:00
|
|
|
getDefaultHash: () => {
|
|
|
|
return defaultRoutesSettingHash;
|
2020-09-09 15:28:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentHash: async (setting) => {
|
|
|
|
const data = await SettingsLoader.loadSettings(setting);
|
|
|
|
|
|
|
|
return calculateHash(JSON.stringify(data));
|
2019-06-25 19:33:56 +03:00
|
|
|
}
|
|
|
|
};
|