mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
fix: Add DATABASE_URL_FILE for loading a db url from a file (#455)
This commit is contained in:
parent
855aae329f
commit
b2ce6f7a48
@ -4,6 +4,7 @@
|
|||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
const serverImpl = require('../lib/server-impl.js');
|
const serverImpl = require('../lib/server-impl.js');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
const argv = require('yargs')
|
const argv = require('yargs')
|
||||||
.usage('$0 [options]')
|
.usage('$0 [options]')
|
||||||
@ -24,10 +25,18 @@ const argv = require('yargs')
|
|||||||
.option('databaseUrl', {
|
.option('databaseUrl', {
|
||||||
alias: 'd',
|
alias: 'd',
|
||||||
describe:
|
describe:
|
||||||
'The full databaseUrl to connect to, including username and password',
|
'The full databaseUrl to connect to, including username and password. Either databaseUrl or databaseUrlFile is required.',
|
||||||
demand: true,
|
demand: false,
|
||||||
type: 'string',
|
type: 'string',
|
||||||
})
|
})
|
||||||
|
.option('databaseUrlFile', {
|
||||||
|
alias: 'f',
|
||||||
|
describe:
|
||||||
|
'The full path to a file containing the full database url to connect to, including username and password. When this option is supplied, it takes precedence over databaseUrl.',
|
||||||
|
demand: false,
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
.check(args => args.databaseUrl || args.databaseUrlFile)
|
||||||
.option('databaseSchema', {
|
.option('databaseSchema', {
|
||||||
alias: 's',
|
alias: 's',
|
||||||
describe: 'The database schema to use',
|
describe: 'The database schema to use',
|
||||||
@ -36,6 +45,11 @@ const argv = require('yargs')
|
|||||||
type: 'string',
|
type: 'string',
|
||||||
}).argv;
|
}).argv;
|
||||||
|
|
||||||
|
if (argv.databaseUrlFile) {
|
||||||
|
argv.databaseUrl = fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
|
||||||
|
delete argv.databaseUrlFile;
|
||||||
|
}
|
||||||
|
|
||||||
serverImpl
|
serverImpl
|
||||||
.start(argv)
|
.start(argv)
|
||||||
.then(instance => {
|
.then(instance => {
|
||||||
|
@ -43,10 +43,10 @@ unleash
|
|||||||
|
|
||||||
Available unleash options include:
|
Available unleash options include:
|
||||||
|
|
||||||
- **databaseUrl** - the postgres database url to connect to. Should include username/password.
|
- **databaseUrl** - the postgres database url to connect to. Should include username/password. This value may also be set via the `DATABASE_URL` environment variable. Alternatively, if you would like to read the database url from a file, you may set the `DATABASE_URL_FILE` environment variable with the full file path. The contents of the file must be the database url exactly.
|
||||||
- **databaseSchema** - the postgres database schema to use. Defaults to 'public'.
|
- **databaseSchema** - the postgres database schema to use. Defaults to 'public'.
|
||||||
- **port** - which port the unleash-server should bind to. If port is omitted or is 0, the operating system will assign an arbitrary unused port. Will be ignored if pipe is specified.
|
- **port** - which port the unleash-server should bind to. If port is omitted or is 0, the operating system will assign an arbitrary unused port. Will be ignored if pipe is specified. This value may also be set via the `HTTP_PORT` environment variable
|
||||||
- **host** - which host the unleash-server should bind to. If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
|
- **host** - which host the unleash-server should bind to. If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise. This value may also be set via the `HTTP_HOST` environment variable
|
||||||
- **pipe** - parameter to identify IPC endpoints. See https://nodejs.org/api/net.html#net_identifying_paths_for_ipc_connections for more details
|
- **pipe** - parameter to identify IPC endpoints. See https://nodejs.org/api/net.html#net_identifying_paths_for_ipc_connections for more details
|
||||||
- **enableLegacyRoutes** (boolean) - allows you to turn on/off support for legacy routes to support older clients. Enabled by default.
|
- **enableLegacyRoutes** (boolean) - allows you to turn on/off support for legacy routes to support older clients. Enabled by default.
|
||||||
- **serverMetrics** (boolean) - use this option to turn on/off prometheus metrics.
|
- **serverMetrics** (boolean) - use this option to turn on/off prometheus metrics.
|
||||||
|
@ -10,10 +10,6 @@ This is a simple `index.js` server file.
|
|||||||
```javascript
|
```javascript
|
||||||
const unleash = require('unleash-server');
|
const unleash = require('unleash-server');
|
||||||
|
|
||||||
if (process.env.DATABASE_URL_FILE) {
|
|
||||||
options.databaseUrl = fs.readFileSync(process.env.DATABASE_URL_FILE);
|
|
||||||
}
|
|
||||||
|
|
||||||
unleash.start(options).then(unleash => {
|
unleash.start(options).then(unleash => {
|
||||||
console.log(`Unleash started on http://localhost:${unleash.app.get('port')}`);
|
console.log(`Unleash started on http://localhost:${unleash.app.get('port')}`);
|
||||||
});
|
});
|
||||||
@ -180,7 +176,6 @@ The `index.js` server file.
|
|||||||
```js
|
```js
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require('fs');
|
|
||||||
const unleash = require('unleash-server');
|
const unleash = require('unleash-server');
|
||||||
const passport = require('@passport-next/passport');
|
const passport = require('@passport-next/passport');
|
||||||
const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2');
|
const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2');
|
||||||
@ -252,10 +247,6 @@ const options = {
|
|||||||
preRouterHook: googleAdminAuth,
|
preRouterHook: googleAdminAuth,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (process.env.DATABASE_URL_FILE) {
|
|
||||||
options.databaseUrl = fs.readFileSync(process.env.DATABASE_URL_FILE);
|
|
||||||
}
|
|
||||||
|
|
||||||
unleash.start(options).then(instance => {
|
unleash.start(options).then(instance => {
|
||||||
console.log(
|
console.log(
|
||||||
`Unleash started on http://localhost:${instance.app.get('port')}`,
|
`Unleash started on http://localhost:${instance.app.get('port')}`,
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const { publicFolder } = require('unleash-frontend');
|
const { publicFolder } = require('unleash-frontend');
|
||||||
const { defaultLogProvider, validateLogProvider } = require('./logger');
|
const { defaultLogProvider, validateLogProvider } = require('./logger');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
const isDev = () => process.env.NODE_ENV === 'development';
|
const isDev = () => process.env.NODE_ENV === 'development';
|
||||||
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
|
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
|
||||||
@ -30,7 +31,9 @@ function defaultOptions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function defaultDatabaseUrl() {
|
function defaultDatabaseUrl() {
|
||||||
if (process.env.DATABASE_URL) {
|
if (process.env.DATABASE_URL_FILE) {
|
||||||
|
return fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
|
||||||
|
} else if (process.env.DATABASE_URL) {
|
||||||
return process.env.DATABASE_URL;
|
return process.env.DATABASE_URL;
|
||||||
} else if (isDev() || process.env.DATABASE_HOST) {
|
} else if (isDev() || process.env.DATABASE_HOST) {
|
||||||
const dbUsername = process.env.DATABASE_USERNAME || 'unleash_user';
|
const dbUsername = process.env.DATABASE_USERNAME || 'unleash_user';
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const test = require('ava');
|
const test = require('ava');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
delete process.env.DATABASE_URL;
|
delete process.env.DATABASE_URL;
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ test('should require DATABASE_URI', t => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should set default databaseUrl for develpment', t => {
|
test('should set default databaseUrl for development', t => {
|
||||||
delete process.env.NODE_ENV;
|
delete process.env.NODE_ENV;
|
||||||
process.env.NODE_ENV = 'development';
|
process.env.NODE_ENV = 'development';
|
||||||
const { createOptions } = require('./options');
|
const { createOptions } = require('./options');
|
||||||
@ -36,6 +37,19 @@ test('should use DATABASE_URL from env', t => {
|
|||||||
t.true(options.databaseUrl === databaseUrl);
|
t.true(options.databaseUrl === databaseUrl);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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 { createOptions } = require('./options');
|
||||||
|
|
||||||
|
const options = createOptions({});
|
||||||
|
|
||||||
|
t.true(options.databaseUrl === databaseUrl);
|
||||||
|
});
|
||||||
|
|
||||||
test('should use databaseUrl from options', t => {
|
test('should use databaseUrl from options', t => {
|
||||||
const databaseUrl = 'postgres://u:p@localhost:5432/name';
|
const databaseUrl = 'postgres://u:p@localhost:5432/name';
|
||||||
const { createOptions } = require('./options');
|
const { createOptions } = require('./options');
|
||||||
|
@ -13,7 +13,7 @@ const User = require('./user');
|
|||||||
const AuthenticationRequired = require('./authentication-required');
|
const AuthenticationRequired = require('./authentication-required');
|
||||||
|
|
||||||
async function createApp(options) {
|
async function createApp(options) {
|
||||||
// Database dependecies (statefull)
|
// Database dependencies (stateful)
|
||||||
const logger = options.getLogger('server-impl.js');
|
const logger = options.getLogger('server-impl.js');
|
||||||
const stores = createStores(options);
|
const stores = createStores(options);
|
||||||
const eventBus = new EventEmitter();
|
const eventBus = new EventEmitter();
|
||||||
|
Loading…
Reference in New Issue
Block a user