2016-11-30 23:41:57 +01:00
|
|
|
'use strict';
|
2016-12-04 14:09:37 +01:00
|
|
|
|
2020-04-13 22:38:46 +02:00
|
|
|
const { readFileSync } = require('fs');
|
|
|
|
const parseDbUrl = require('parse-database-url');
|
|
|
|
const merge = require('deepmerge');
|
2016-12-02 16:34:05 +01:00
|
|
|
const { publicFolder } = require('unleash-frontend');
|
2019-04-30 21:14:23 +02:00
|
|
|
const { defaultLogProvider, validateLogProvider } = require('./logger');
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2017-11-16 15:41:33 +01:00
|
|
|
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
|
2016-12-03 14:09:09 +01:00
|
|
|
|
2019-05-22 09:29:56 +02:00
|
|
|
function defaultOptions() {
|
|
|
|
return {
|
|
|
|
databaseUrl: defaultDatabaseUrl(),
|
|
|
|
databaseSchema: 'public',
|
2020-04-13 22:38:46 +02:00
|
|
|
db: {
|
|
|
|
user: process.env.DATABASE_USERNAME,
|
|
|
|
password: process.env.DATABASE_PASSWORD,
|
|
|
|
host: process.env.DATABASE_HOST,
|
|
|
|
port: process.env.DATABASE_PORT || 5432,
|
|
|
|
database: process.env.DATABASE_NAME || 'unleash',
|
|
|
|
ssl: process.env.DATABASE_SSL,
|
|
|
|
driver: 'postgres',
|
|
|
|
},
|
2019-05-22 09:29:56 +02:00
|
|
|
port: process.env.HTTP_PORT || process.env.PORT || 4242,
|
|
|
|
host: process.env.HTTP_HOST,
|
|
|
|
pipe: undefined,
|
|
|
|
baseUriPath: process.env.BASE_URI_PATH || '',
|
|
|
|
serverMetrics: true,
|
2020-04-13 22:43:14 +02:00
|
|
|
enableLegacyRoutes: false,
|
2019-05-22 09:29:56 +02:00
|
|
|
extendedPermissions: false,
|
|
|
|
publicFolder,
|
2020-04-13 22:38:46 +02:00
|
|
|
enableRequestLogger: false,
|
2019-05-22 09:29:56 +02:00
|
|
|
sessionAge: THIRTY_DAYS,
|
|
|
|
adminAuthentication: 'unsecure',
|
|
|
|
ui: {},
|
|
|
|
importFile: undefined,
|
|
|
|
dropBeforeImport: false,
|
|
|
|
getLogger: defaultLogProvider,
|
2019-10-13 20:21:04 +02:00
|
|
|
customContextFields: [],
|
2019-11-18 08:10:58 +01:00
|
|
|
disableDBMigration: false,
|
2019-05-22 09:29:56 +02:00
|
|
|
};
|
|
|
|
}
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2019-05-22 08:24:22 +02:00
|
|
|
function defaultDatabaseUrl() {
|
2019-06-14 18:10:38 +02:00
|
|
|
if (process.env.DATABASE_URL_FILE) {
|
2020-04-13 22:38:46 +02:00
|
|
|
return readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
|
2019-06-14 18:10:38 +02:00
|
|
|
} else if (process.env.DATABASE_URL) {
|
2019-05-22 09:29:56 +02:00
|
|
|
return process.env.DATABASE_URL;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-22 08:24:22 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
module.exports = {
|
2020-04-13 22:38:46 +02:00
|
|
|
createOptions: (opts = {}) => {
|
|
|
|
const options = merge(defaultOptions(), opts);
|
2016-12-01 00:42:14 +01:00
|
|
|
|
2020-04-13 22:38:46 +02:00
|
|
|
// Use DATABASE_URL when 'db' not defined.
|
|
|
|
if (!opts.db && options.databaseUrl) {
|
|
|
|
options.db = parseDbUrl(options.databaseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.db.host) {
|
2017-06-28 10:17:14 +02:00
|
|
|
throw new Error(
|
2020-04-13 22:38:46 +02:00
|
|
|
`Unleash requires database details to start. See https://unleash.github.io/docs/getting_started`
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2016-11-30 23:41:57 +01:00
|
|
|
}
|
2019-03-12 10:46:08 +01:00
|
|
|
|
2019-04-26 08:45:45 +02:00
|
|
|
options.listen = options.pipe
|
|
|
|
? { path: options.pipe }
|
|
|
|
: { port: options.port, host: options.host };
|
|
|
|
|
2019-04-30 21:14:23 +02:00
|
|
|
validateLogProvider(options.getLogger);
|
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
return options;
|
2016-12-04 14:09:37 +01:00
|
|
|
},
|
|
|
|
};
|