Simplify configuration for different environments

This commit is contained in:
Tankred Hase
2016-06-07 14:56:55 +02:00
parent 2ab6333a15
commit 2acbffa2f2
18 changed files with 94 additions and 158 deletions

View File

@@ -95,42 +95,15 @@ app.on('error', (error, ctx) => {
//
function injectDependencies() {
let credentials = readCredentials();
mongo = new Mongo({
uri: process.env.MONGO_URI || credentials.mongo.uri,
user: process.env.MONGO_USER || credentials.mongo.user,
password: process.env.MONGO_PASS || credentials.mongo.pass
});
mongo = new Mongo(config.mongo);
email = new Email(nodemailer, openpgpEncrypt);
email.init({
host: process.env.SMTP_HOST || credentials.smtp.host,
port: process.env.SMTP_PORT || credentials.smtp.port,
tls: (process.env.SMTP_TLS || credentials.smtp.tls) === 'true',
starttls: (process.env.SMTP_STARTTLS || credentials.smtp.starttls) === 'true',
pgp: (process.env.SMTP_PGP || credentials.smtp.pgp) === 'true',
auth: {
user: process.env.SMTP_USER || credentials.smtp.user,
pass: process.env.SMTP_PASS || credentials.smtp.pass
},
sender: {
name: process.env.SENDER_NAME || credentials.sender.name,
email: process.env.SENDER_EMAIL || credentials.sender.email
}
});
email.init(config.email);
userId = new UserId(mongo);
publicKey = new PublicKey(openpgp, mongo, email, userId);
hkp = new HKP(publicKey);
rest = new REST(publicKey, userId);
}
function readCredentials() {
try {
return require('../credentials.json');
} catch(e) {
log.info('app', 'No credentials.json found ... using environment vars.');
}
}
//
// Start app ... connect to the database and start listening
//

View File

@@ -26,12 +26,12 @@ class Mongo {
/**
* Create an instance of the MongoDB client.
* @param {String} uri The mongodb uri
* @param {String} user The databse user
* @param {String} password The database user's password
* @param {String} uri The mongodb uri
* @param {String} user The databse user
* @param {String} pass The database user's password
*/
constructor(options) {
this._uri = 'mongodb://' + options.user + ':' + options.password + '@' + options.uri;
this._uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri;
}
/**

View File

@@ -49,10 +49,10 @@ class Email {
host: options.host,
port: options.port || 465,
auth: options.auth,
secure: (options.tls !== undefined) ? options.tls : true,
requireTLS: (options.starttls !== undefined) ? options.starttls : true,
secure: (options.tls !== undefined) ? util.isTrue(options.tls) : true,
requireTLS: (options.starttls !== undefined) ? util.isTrue(options.starttls) : true,
});
if (options.pgp) {
if (util.isTrue(options.pgp)) {
this._transport.use('stream', this._openpgpEncrypt());
}
this._sender = options.sender;

View File

@@ -28,6 +28,19 @@ exports.isString = function(data) {
return typeof data === 'string' || String.prototype.isPrototypeOf(data);
};
/**
* Cast string to a boolean value
* @param {} data The input to be checked
* @return {boolean} If data is true
*/
exports.isTrue = function(data) {
if (this.isString(data)) {
return data === 'true';
} else {
return !!data;
}
};
/**
* Checks for a valid key id which is between 8 and 40 hex chars.
* @param {string} data The key id