Ghost/core/server/api/configuration.js
Katharina Irrgang 0201c431d7 🔥 do not store settings in config (#7924)
* 🎨  🔥  do not store settings in config and make settings cache easier available

- remove remembering settings value in theme config
- if we need a cache value, we are asking the settings cache directly
- instead of settings.getSettingSync we use settings.cache.get

- added TODO:
  - think about moving the settings cache out of api/settings
  - we could create a folder named cache cache/settings
  - this settings cache listens on model changes for settings
  - decoupling

* 🔥  remove timezone from config

- no need to store in overrides config and in defaults settings

* 🎨  context object helper

- replace config.get('theme') by settings cache

* 🎨  replace config.get('theme') by settings.cache.get

* 🎨  adapt tests

* fixes from comments
2017-02-03 13:15:11 +00:00

96 lines
3.1 KiB
JavaScript

// # Configuration API
// RESTful API for browsing the configuration
var _ = require('lodash'),
config = require('../config'),
settingsCache = require('../api/settings').cache,
ghostVersion = require('../utils/ghost-version'),
models = require('../models'),
Promise = require('bluebird'),
utils = require('../utils'),
configuration;
function fetchAvailableTimezones() {
var timezones = require('../data/timezones.json');
return timezones;
}
function getAboutConfig() {
return {
version: ghostVersion.full,
environment: config.get('env'),
database: config.get('database').client,
mail: _.isObject(config.get('mail')) ? config.get('mail').transport : ''
};
}
function getBaseConfig() {
return {
fileStorage: config.get('fileStorage') !== false,
useGravatar: !config.isPrivacyDisabled('useGravatar'),
publicAPI: config.get('publicAPI') === true,
blogUrl: utils.url.urlFor('home', true),
blogTitle: settingsCache.get('title'),
routeKeywords: config.get('routeKeywords')
};
}
/**
* ## Configuration API Methods
*
* We need to load the client credentials dynamically.
* For example: on bootstrap ghost-auth get's created and if we load them here in parallel,
* it can happen that we won't get any client credentials or wrong credentials.
*
* **See:** [API Methods](index.js.html#api%20methods)
*/
configuration = {
/**
* Always returns {configuration: []}
* Sometimes the array contains configuration items
* @param {Object} options
* @returns {Promise<Object>}
*/
read: function read(options) {
options = options || {};
var ops = {};
if (!options.key) {
ops.ghostAdmin = models.Client.findOne({slug: 'ghost-admin'});
if (config.get('auth:type') === 'ghost') {
ops.ghostAuth = models.Client.findOne({slug: 'ghost-auth'});
}
return Promise.props(ops)
.then(function (result) {
var configuration = getBaseConfig();
configuration.clientId = result.ghostAdmin.get('slug');
configuration.clientSecret = result.ghostAdmin.get('secret');
if (config.get('auth:type') === 'ghost') {
configuration.ghostAuthId = result.ghostAuth && result.ghostAuth.get('uuid') || 'not-available';
configuration.ghostAuthUrl = config.get('auth:url');
}
return {configuration: [configuration]};
});
}
if (options.key === 'about') {
return Promise.resolve({configuration: [getAboutConfig()]});
}
// Timezone endpoint
if (options.key === 'timezones') {
return Promise.resolve({configuration: [fetchAvailableTimezones()]});
}
return Promise.resolve({configuration: []});
}
};
module.exports = configuration;