diff --git a/lib/options.js b/lib/options.js index f6b829031d..e58ed1af9c 100644 --- a/lib/options.js +++ b/lib/options.js @@ -7,7 +7,10 @@ const isDev = () => process.env.NODE_ENV === 'development'; const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000; const DEFAULT_OPTIONS = { - databaseUrl: process.env.DATABASE_URL, + databaseUrl: + process.env.DATABASE_URL || process.env.DATABASE_HOST + ? defaultDatabaseUrl() + : null, databaseSchema: 'public', port: process.env.HTTP_PORT || process.env.PORT || 4242, host: process.env.HTTP_HOST, @@ -27,19 +30,28 @@ const DEFAULT_OPTIONS = { getLogger: defaultLogProvider, }; +function defaultDatabaseUrl() { + const dbUsername = process.env.DATABASE_USERNAME || 'unleash_user'; + const dbPassword = process.env.DATABASE_PASSWORD || 'passord'; + const dbHost = process.env.DATABASE_HOST || 'localhost'; + const dbPort = process.env.DATABASE_PORT || 5432; + const dbName = process.env.DATABASE_NAME || 'unleash'; + const sslSupport = process.env.DATABASE_SSL || 'true'; + return `postgres://${dbUsername}:${dbPassword}@${dbHost}:${dbPort}/${dbName}?ssl=${sslSupport}`; +} + module.exports = { createOptions: opts => { const options = Object.assign({}, DEFAULT_OPTIONS, opts); // If we are running in development we should assume local db if (isDev() && !options.databaseUrl) { - options.databaseUrl = - 'postgres://unleash_user:passord@localhost:5432/unleash'; + options.databaseUrl = defaultDatabaseUrl(); } if (!options.databaseUrl) { throw new Error( - 'You must either pass databaseUrl option or set environemnt variable DATABASE_URL' + 'You must either pass databaseUrl option or set environemnt variable DATABASE_URL || (DATABASE_HOST, DATABASE_PORT, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME)' ); } diff --git a/lib/options.test.js b/lib/options.test.js index 60b28e6b7a..d2785f5e58 100644 --- a/lib/options.test.js +++ b/lib/options.test.js @@ -20,7 +20,7 @@ test('should set default databaseUrl for develpment', t => { t.true( options.databaseUrl === - 'postgres://unleash_user:passord@localhost:5432/unleash' + 'postgres://unleash_user:passord@localhost:5432/unleash?ssl=true' ); });