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:
parent
b4bb171d7b
commit
484b10029d
@ -7,7 +7,10 @@ const isDev = () => process.env.NODE_ENV === 'development';
|
|||||||
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
|
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
databaseUrl: process.env.DATABASE_URL,
|
databaseUrl:
|
||||||
|
process.env.DATABASE_URL || process.env.DATABASE_HOST
|
||||||
|
? defaultDatabaseUrl()
|
||||||
|
: null,
|
||||||
databaseSchema: 'public',
|
databaseSchema: 'public',
|
||||||
port: process.env.HTTP_PORT || process.env.PORT || 4242,
|
port: process.env.HTTP_PORT || process.env.PORT || 4242,
|
||||||
host: process.env.HTTP_HOST,
|
host: process.env.HTTP_HOST,
|
||||||
@ -27,19 +30,28 @@ const DEFAULT_OPTIONS = {
|
|||||||
getLogger: defaultLogProvider,
|
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 = {
|
module.exports = {
|
||||||
createOptions: opts => {
|
createOptions: opts => {
|
||||||
const options = Object.assign({}, DEFAULT_OPTIONS, opts);
|
const options = Object.assign({}, DEFAULT_OPTIONS, opts);
|
||||||
|
|
||||||
// If we are running in development we should assume local db
|
// If we are running in development we should assume local db
|
||||||
if (isDev() && !options.databaseUrl) {
|
if (isDev() && !options.databaseUrl) {
|
||||||
options.databaseUrl =
|
options.databaseUrl = defaultDatabaseUrl();
|
||||||
'postgres://unleash_user:passord@localhost:5432/unleash';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.databaseUrl) {
|
if (!options.databaseUrl) {
|
||||||
throw new Error(
|
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)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ test('should set default databaseUrl for develpment', t => {
|
|||||||
|
|
||||||
t.true(
|
t.true(
|
||||||
options.databaseUrl ===
|
options.databaseUrl ===
|
||||||
'postgres://unleash_user:passord@localhost:5432/unleash'
|
'postgres://unleash_user:passord@localhost:5432/unleash?ssl=true'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user