mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-23 00:22:19 +01:00
fix: readd support for DATABASE_URL_FILE
This commit is contained in:
parent
4752d5f0de
commit
447e44ee77
@ -1,5 +1,6 @@
|
|||||||
import { parse } from 'pg-connection-string';
|
import { parse } from 'pg-connection-string';
|
||||||
import merge from 'deepmerge';
|
import merge from 'deepmerge';
|
||||||
|
import * as fs from 'fs';
|
||||||
import {
|
import {
|
||||||
IUnleashOptions,
|
IUnleashOptions,
|
||||||
IUnleashConfig,
|
IUnleashConfig,
|
||||||
@ -157,9 +158,23 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
|
|||||||
} else if (process.env.DATABASE_URL) {
|
} else if (process.env.DATABASE_URL) {
|
||||||
extraDbOptions = parse(process.env.DATABASE_URL);
|
extraDbOptions = parse(process.env.DATABASE_URL);
|
||||||
}
|
}
|
||||||
|
let fileDbOptions = {};
|
||||||
|
if (options.databaseUrlFile && fs.existsSync(options.databaseUrlFile)) {
|
||||||
|
fileDbOptions = parse(
|
||||||
|
fs.readFileSync(options.databaseUrlFile, 'utf-8'),
|
||||||
|
);
|
||||||
|
} else if (
|
||||||
|
process.env.DATABASE_URL_FILE &&
|
||||||
|
fs.existsSync(process.env.DATABASE_URL_FILE)
|
||||||
|
) {
|
||||||
|
fileDbOptions = parse(
|
||||||
|
fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf-8'),
|
||||||
|
);
|
||||||
|
}
|
||||||
const db: IDBOption = mergeAll<IDBOption>([
|
const db: IDBOption = mergeAll<IDBOption>([
|
||||||
defaultDbOptions,
|
defaultDbOptions,
|
||||||
dbPort(extraDbOptions),
|
dbPort(extraDbOptions),
|
||||||
|
dbPort(fileDbOptions),
|
||||||
options.db,
|
options.db,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -73,6 +73,7 @@ export interface IServerOption {
|
|||||||
|
|
||||||
export interface IUnleashOptions {
|
export interface IUnleashOptions {
|
||||||
databaseUrl?: string;
|
databaseUrl?: string;
|
||||||
|
databaseUrlFile?: string;
|
||||||
db?: Partial<IDBOption>;
|
db?: Partial<IDBOption>;
|
||||||
session?: Partial<ISessionOption>;
|
session?: Partial<ISessionOption>;
|
||||||
getLogger?: LogProvider;
|
getLogger?: LogProvider;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import * as fs from 'fs';
|
||||||
import { createConfig, authTypeFromString } from '../../lib/create-config';
|
import { createConfig, authTypeFromString } from '../../lib/create-config';
|
||||||
import { IAuthType, IDBOption } from '../../lib/types/option';
|
import { IAuthType, IDBOption } from '../../lib/types/option';
|
||||||
|
|
||||||
@ -96,3 +97,18 @@ test('Can set auth type programmatically with a string', t => {
|
|||||||
});
|
});
|
||||||
t.is(config.authentication.type, IAuthType.DEMO);
|
t.is(config.authentication.type, IAuthType.DEMO);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should use DATABASE_URL_FILE from env', t => {
|
||||||
|
const databaseUrl = 'postgres://u:p@localhost:5432/name';
|
||||||
|
const path = '/tmp/db_url';
|
||||||
|
fs.writeFileSync(path, databaseUrl, { mode: 0o755 });
|
||||||
|
delete process.env.NODE_ENV;
|
||||||
|
process.env.DATABASE_URL_FILE = path;
|
||||||
|
const config = createConfig({});
|
||||||
|
|
||||||
|
t.is(config.db.host, 'localhost');
|
||||||
|
t.is(config.db.password, 'p');
|
||||||
|
t.is(config.db.user, 'u');
|
||||||
|
t.is(config.db.database, 'name');
|
||||||
|
t.is(config.db.schema, 'public');
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user