app2-api/core/configurations.js
2019-04-15 12:13:17 +02:00

59 lines
1.5 KiB
JavaScript

'use strict';
// Dependencies.
const glob = require('glob');
const { get, upperFirst, camelCase } = require('lodash');
const utils = require('../utils');
const models = require('./models');
module.exports.nested = function() {
return Promise.all([
// Load root configurations.
new Promise((resolve, reject) => {
glob('./config/index.js', {
cwd: this.config.appPath,
dot: true
}, (err, files) => {
if (err) {
return reject(err);
}
utils.loadConfig.call(this, files).then(resolve).catch(reject);
});
})
]);
};
module.exports.app = async function() {
// Set connections.
this.connections = {};
// Set current environment config.
this.config.currentEnvironment = this.config.environments[this.config.environment] || {};
// default settings
this.config.port = get(this.config.currentEnvironment, 'server.port') || this.config.port;
this.config.host = get(this.config.currentEnvironment, 'server.host') || this.config.host;
// Set current URL
this.config.url = getURLFromSegments({
hostname: this.config.host,
port: this.config.port
});
// Set models
this.models = models.call(this);
};
const getURLFromSegments = function ({ hostname, port, ssl = false }) {
const protocol = ssl ? 'https' : 'http';
const defaultPort = ssl ? 443 : 80;
const portString = (port === undefined || parseInt(port) === defaultPort) ? '' : `:${port}`;
return `${protocol}://${hostname}${portString}`;
};