Ghost/core/server/config/utils.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

var path = require('path'),
fs = require('fs-extra'),
_ = require('lodash');
exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
if (!this.get('privacy')) {
return false;
}
// CASE: disable all privacy features
if (this.get('privacy').useTinfoil === true) {
// CASE: you can still enable single features
if (this.get('privacy')[privacyFlag] === true) {
return false;
}
return true;
}
return this.get('privacy')[privacyFlag] === false;
};
/**
* transform all relative paths to absolute paths
* @TODO: re-write this function a little bit so we don't have to add the parent path - that is hard to understand
*
* Path must be string.
* Path must match minimum one / or \
* Path can be a "." to re-present current folder
*/
exports.makePathsAbsolute = function makePathsAbsolute(obj, parent) {
var self = this;
_.each(obj, function (configValue, pathsKey) {
if (_.isObject(configValue)) {
makePathsAbsolute.bind(self)(configValue, parent + ':' + pathsKey);
} else if (
_.isString(configValue) &&
(configValue.match(/\/+|\\+/) || configValue === '.') &&
!path.isAbsolute(configValue)
) {
self.set(parent + ':' + pathsKey, path.normalize(path.join(__dirname, '../../..', configValue)));
}
});
};
/**
* we can later support setting folder names via custom config values
*/
exports.getContentPath = function getContentPath(type) {
switch (type) {
case 'images':
return path.join(this.get('paths:contentPath'), 'images/');
case 'apps':
return path.join(this.get('paths:contentPath'), 'apps/');
case 'themes':
return path.join(this.get('paths:contentPath'), 'themes/');
case 'storage':
return path.join(this.get('paths:contentPath'), 'adapters', 'storage/');
case 'scheduling':
return path.join(this.get('paths:contentPath'), 'adapters', 'scheduling/');
🎨 logging improvements (#7597) * 🎨 rotation config - every parameter is configureable - increase default number of files to 100 * 🎨 ghost.log location - example: content/logs/http___my_ghost_blog_com_ghost.log - user can change the path to something custom by setting logging.path * 🛠 add response-time as dependency * 🎨 readable PrettyStream - tidy up - generic handling (was important to support more use cases, for example: logging.info({ anyKey: anyValue })) - common log format - less code 🕵🏻 * 🎨 GhostLogger cleanup - remove setLoggers -> this function had too much of redundant code - instead: add smart this.log function - remove logging.request (---> GhostLogger just forwards the values, it doesn't matter if that is a request or not a request) - make .warn .debug .info .error small and smart * 🎨 app.js: add response time as middleware and remove logging.request * 🎨 setStdoutStream and setFileStream - redesign GhostLogger to add CustomLoggers very easily ----> Example CustomLogger function CustomLogger(options) { // Base iterates over defined transports // EXAMPLE: ['stdout', 'elasticsearch'] Base.call(this, options); } util.inherits(...); // OVERRIDE default stdout stream and your own!!! CustomLogger.prototype.setStdoutStream = function() {} // add a new stream // get's called automatically when transport elasticsearch is defined CustomLogger.prototype.setElasticsearchStream = function() {} * 🎨 log into multiple file by default - content/logs/domain.error.log --> contains only the errors - content/logs/domain.log --> contains everything - rotation for both files * 🔥 remove logging.debug and use npm debug only * ✨ shortcuts for mode and level * 🎨 jshint/jscs * 🎨 stdout as much as possible for an error * 🎨 fix tests * 🎨 remove req.ip from log output, remove response-time dependency * 🎨 create middleware for logging - added TODO to move logging middleware to ignition
2016-10-25 14:17:43 +03:00
case 'logs':
return path.join(this.get('paths:contentPath'), 'logs/');
case 'data':
return path.join(this.get('paths:contentPath'), 'data/');
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 04:34:03 +03:00
case 'settings':
return path.join(this.get('paths:contentPath'), 'settings/');
default:
throw new Error('getContentPath was called with: ' + type);
}
};
/**
* @TODO:
* - content/logs folder is required right now, otherwise Ghost want start
*/
exports.doesContentPathExist = function doesContentPathExist() {
if (!fs.pathExistsSync(this.get('paths:contentPath'))) {
throw new Error('Your content path does not exist! Please double check `paths.contentPath` in your custom config file e.g. config.production.json.');
}
};
/**
* Check if the URL in config has a protocol and sanitise it if not including a warning that it should be changed
*/
exports.checkUrlProtocol = function checkUrlProtocol() {
var url = this.get('url');
if (!url.match(/^https?:\/\//i)) {
throw new Error('URL in config must be provided with protocol, eg. "http://my-ghost-blog.com"');
}
};
/**
* nconf merges all database keys together and this can be confusing
* e.g. production default database is sqlite, but you override the configuration with mysql
*
* this.clear('key') does not work
* https://github.com/indexzero/nconf/issues/235#issuecomment-257606507
*/
exports.sanitizeDatabaseProperties = function sanitizeDatabaseProperties() {
var database = this.get('database');
if (this.get('database:client') === 'mysql') {
delete database.connection.filename;
} else {
delete database.connection.host;
delete database.connection.user;
delete database.connection.password;
delete database.connection.database;
}
this.set('database', database);
};