1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: Separate DATABASE_URL to multiple DATABASE_* variable (#437)

* Support construct database by multiple env variable
* fix typo for test
* Add small change for passord and support ssl
* consider ssl should be true always
This commit is contained in:
Thinh Tran 2019-05-22 13:24:22 +07:00 committed by Ivar Conradi Østhus
parent b4bb171d7b
commit 484b10029d
2 changed files with 17 additions and 5 deletions

View File

@ -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)'
);
}

View File

@ -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'
);
});