2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Extracted settings service part manipulating routes.yaml (#10800)

refs #10790
refs #9528

- The settings service was designed to handle more settings then just routing, but till this day there wasn't anything else added. As routes.yaml is only being used by frontend router so conceptually it fits better to have this code in frontend, so that it doesn't have to reach out to server
- The code left in server settings is the one that interacts with the database `settings` table and only partially provides information to frontend. That part is known as 'settings cache' and will be accessed through API controllers.
This commit is contained in:
Naz Gargol 2019-06-25 18:33:56 +02:00 committed by GitHub
parent c3a493bfeb
commit 0bf1542bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 114 additions and 100 deletions

View file

@ -1,7 +1,7 @@
const debug = require('ghost-ignition').debug('services:routing:bootstrap');
const _ = require('lodash');
const common = require('../../../server/lib/common');
const settingsService = require('../../../server/services/settings');
const settingsService = require('../settings');
const themeService = require('../themes');
const StaticRoutesRouter = require('./StaticRoutesRouter');
const StaticPagesRouter = require('./StaticPagesRouter');

View file

@ -2,8 +2,8 @@ const fs = require('fs-extra'),
Promise = require('bluebird'),
path = require('path'),
debug = require('ghost-ignition').debug('services:settings:ensure-settings'),
common = require('../../lib/common'),
config = require('../../config');
common = require('../../../server/lib/common'),
config = require('../../../server/config');
/**
* Makes sure that all supported settings files are in the

View file

@ -0,0 +1,80 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('frontend:services:settings:index');
const SettingsLoader = require('./loader');
const ensureSettingsFiles = require('./ensure-settings');
const common = require('../../../server/lib/common');
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 common.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(),
settingsToReturn = {};
_.each(knownSettings, function (setting) {
settingsToReturn[setting] = SettingsLoader(setting);
});
return settingsToReturn;
}
};

View file

@ -1,8 +1,8 @@
const fs = require('fs-extra'),
path = require('path'),
debug = require('ghost-ignition').debug('services:settings:settings-loader'),
common = require('../../lib/common'),
config = require('../../config'),
common = require('../../../server/lib/common'),
config = require('../../../server/config'),
yamlParser = require('./yaml-parser'),
validate = require('./validate');

View file

@ -1,7 +1,7 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('services:settings:validate');
const common = require('../../lib/common');
const themeService = require('../../../frontend/services/themes');
const common = require('../../../server/lib/common');
const themeService = require('../themes');
const _private = {};
let RESOURCE_CONFIG;

View file

@ -1,6 +1,6 @@
const yaml = require('js-yaml'),
debug = require('ghost-ignition').debug('services:settings:yaml-parser'),
common = require('../../lib/common');
common = require('../../../server/lib/common');
/**
* Takes a YAML file, parses it and returns a JSON Object

View file

@ -6,7 +6,7 @@
"helperTemplates": "core/frontend/helpers/tpl/",
"adminViews": "core/server/web/admin/views/",
"defaultViews": "core/server/views/",
"defaultSettings": "core/server/services/settings/",
"defaultSettings": "core/frontend/services/settings/",
"internalAppPath": "core/frontend/apps/",
"internalStoragePath": "core/server/adapters/storage/",
"internalSchedulingPath": "core/server/adapters/scheduling/",

View file

@ -77,6 +77,7 @@ function initialiseServices() {
const minimalRequiredSetupToStartGhost = (dbState) => {
const settings = require('./services/settings');
const models = require('./models');
const frontendSettings = require('../frontend/services/settings');
const themes = require('../frontend/services/themes');
const GhostServer = require('./ghost-server');
@ -92,6 +93,11 @@ const minimalRequiredSetupToStartGhost = (dbState) => {
return settings.init()
.then(() => {
debug('Settings done');
return frontendSettings.init();
})
.then(() => {
debug('Frontend settings done');
return themes.init();
})
.then(() => {

View file

@ -2,93 +2,17 @@
* Settings Lib
* A collection of utilities for handling settings including a cache
*/
const _ = require('lodash'),
debug = require('ghost-ignition').debug('services:settings:index'),
common = require('../../lib/common'),
models = require('../../models'),
SettingsCache = require('./cache'),
SettingsLoader = require('./loader'),
ensureSettingsFiles = require('./ensure-settings');
const models = require('../../models');
const SettingsCache = require('./cache');
module.exports = {
init: function init() {
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)
.then(() => {
// Update the defaults
return models.Settings.populateDefaults();
})
// Update the defaults
return models.Settings.populateDefaults()
.then((settingsCollection) => {
// Initialise the cache with the result
// This will bind to events for further updates
SettingsCache.init(settingsCollection);
});
},
/**
* 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 common.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(),
settingsToReturn = {};
_.each(knownSettings, function (setting) {
settingsToReturn[setting] = SettingsLoader(setting);
});
return settingsToReturn;
}
};

View file

@ -6,9 +6,9 @@ const sinon = require('sinon'),
configUtils = require('../../../utils/configUtils'),
common = require('../../../../server/lib/common'),
ensureSettings = require('../../../../server/services/settings/ensure-settings');
ensureSettings = require('../../../../frontend/services/settings/ensure-settings');
describe('UNIT > Settings Service:', function () {
describe('UNIT > Settings Service ensure settings:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
sinon.stub(fs, 'readFile');
@ -32,7 +32,7 @@ describe('UNIT > Settings Service:', function () {
});
it('copies default settings file if not found but does not overwrite existing files', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-globals.yaml');
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../frontend/services/settings/default-globals.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/globals.yaml');
const fsError = new Error('not found');
fsError.code = 'ENOENT';
@ -49,7 +49,7 @@ describe('UNIT > Settings Service:', function () {
});
it('copies default settings file if no file found', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-routes.yaml');
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../frontend/services/settings/default-routes.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
const fsError = new Error('not found');
fsError.code = 'ENOENT';

View file

@ -5,9 +5,9 @@ const sinon = require('sinon'),
path = require('path'),
configUtils = require('../../../utils/configUtils'),
common = require('../../../../server/lib/common'),
loadSettings = rewire('../../../../server/services/settings/loader');
loadSettings = rewire('../../../../frontend/services/settings/loader');
describe('UNIT > Settings Service:', function () {
describe('UNIT > Settings Service loader:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
});

View file

@ -2,7 +2,7 @@ const sinon = require('sinon'),
should = require('should'),
rewire = require('rewire'),
common = require('../../../../server/lib/common'),
settings = rewire('../../../../server/services/settings');
settings = rewire('../../../../frontend/services/settings');
describe('UNIT > Settings Service:', function () {
afterEach(function () {

View file

@ -2,7 +2,7 @@ const should = require('should');
const sinon = require('sinon');
const common = require('../../../../server/lib/common');
const themesService = require('../../../../frontend/services/themes');
const validate = require('../../../../server/services/settings/validate');
const validate = require('../../../../frontend/services/settings/validate');
should.equal(true, true);

View file

@ -4,9 +4,9 @@ const sinon = require('sinon'),
yaml = require('js-yaml'),
path = require('path'),
yamlParser = require('../../../../server/services/settings/yaml-parser');
yamlParser = require('../../../../frontend/services/settings/yaml-parser');
describe('UNIT > Settings Service:', function () {
describe('UNIT > Settings Service yaml parser:', function () {
let yamlSpy;
beforeEach(function () {

View file

@ -22,6 +22,7 @@ var Promise = require('bluebird'),
urlService = require('../../frontend/services/url'),
routingService = require('../../frontend/services/routing'),
settingsService = require('../../server/services/settings'),
frontendSettingsService = require('../../frontend/services/settings'),
settingsCache = require('../../server/services/settings/cache'),
imageLib = require('../../server/lib/image'),
web = require('../../server/web'),
@ -895,6 +896,9 @@ startGhost = function startGhost(options) {
settingsCache.shutdown();
return settingsService.init();
})
.then(function () {
return frontendSettingsService.init();
})
.then(function () {
return themes.init();
})
@ -1109,7 +1113,7 @@ module.exports = {
},
init: function () {
const routes = settingsService.get('routes');
const routes = frontendSettingsService.get('routes');
const collectionRouter = new routingService.CollectionRouter('/', routes.collections['/']);
const tagRouter = new routingService.TaxonomyRouter('tag', routes.taxonomies.tag);