2014-08-21 01:42:34 +04:00
|
|
|
// # Configuration API
|
|
|
|
// RESTful API for browsing the configuration
|
|
|
|
var _ = require('lodash'),
|
|
|
|
config = require('../config'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
Promise = require('bluebird'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-08-21 01:42:34 +04:00
|
|
|
|
|
|
|
configuration;
|
|
|
|
|
2016-01-19 18:43:09 +03:00
|
|
|
function labsFlag(key) {
|
|
|
|
return {
|
|
|
|
value: (config[key] === true),
|
|
|
|
type: 'bool'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-08-21 01:42:34 +04:00
|
|
|
function getValidKeys() {
|
|
|
|
var validKeys = {
|
2016-01-19 18:43:09 +03:00
|
|
|
fileStorage: {value: (config.fileStorage !== false), type: 'bool'},
|
|
|
|
publicAPI: labsFlag('publicAPI'),
|
|
|
|
apps: {value: (config.apps === true), type: 'bool'},
|
|
|
|
version: {value: config.ghostVersion, type: 'string'},
|
2014-09-10 08:06:24 +04:00
|
|
|
environment: process.env.NODE_ENV,
|
|
|
|
database: config.database.client,
|
|
|
|
mail: _.isObject(config.mail) ? config.mail.transport : '',
|
2016-01-19 18:43:09 +03:00
|
|
|
blogUrl: {value: config.url.replace(/\/$/, ''), type: 'string'},
|
|
|
|
blogTitle: {value: config.theme.title, type: 'string'},
|
|
|
|
routeKeywords: {value: JSON.stringify(config.routeKeywords), type: 'json'}
|
2014-08-21 01:42:34 +04:00
|
|
|
};
|
|
|
|
|
2014-11-28 20:47:32 +03:00
|
|
|
return validKeys;
|
2014-08-21 01:42:34 +04:00
|
|
|
}
|
|
|
|
|
2016-01-19 18:43:09 +03:00
|
|
|
function formatConfigurationObject(val, key) {
|
|
|
|
return {
|
|
|
|
key: key,
|
|
|
|
value: (_.isObject(val) && _.has(val, 'value')) ? val.value : val,
|
|
|
|
type: _.isObject(val) ? (val.type || null) : null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-08-21 01:42:34 +04:00
|
|
|
/**
|
|
|
|
* ## Configuration API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
|
|
|
configuration = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Browse
|
|
|
|
* Fetch all configuration keys
|
|
|
|
* @returns {Promise(Configurations)}
|
|
|
|
*/
|
2014-09-01 23:44:13 +04:00
|
|
|
browse: function browse() {
|
2016-01-19 18:43:09 +03:00
|
|
|
return Promise.resolve({configuration: _.map(getValidKeys(), formatConfigurationObject)});
|
2014-08-21 01:42:34 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Read
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
read: function read(options) {
|
2014-11-28 20:47:32 +03:00
|
|
|
var data = getValidKeys();
|
|
|
|
|
|
|
|
if (_.has(data, options.key)) {
|
2016-01-19 18:43:09 +03:00
|
|
|
return Promise.resolve({configuration: [formatConfigurationObject(data[options.key], options.key)]});
|
2014-11-28 20:47:32 +03:00
|
|
|
} else {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError(i18n.t('errors.api.configuration.invalidKey')));
|
2014-11-28 20:47:32 +03:00
|
|
|
}
|
2014-08-21 01:42:34 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = configuration;
|