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');
|
2020-08-03 13:34:10 +02:00
|
|
|
const version = require('./util/version');
|
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
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
function defaultDatabaseUrl() {
|
|
|
|
if (process.env.DATABASE_URL_FILE) {
|
|
|
|
return readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
|
|
|
|
}
|
|
|
|
if (process.env.DATABASE_URL) {
|
|
|
|
return process.env.DATABASE_URL;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
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',
|
2020-04-18 16:29:05 +02:00
|
|
|
ssl:
|
|
|
|
process.env.DATABASE_SSL != null
|
|
|
|
? JSON.parse(process.env.DATABASE_SSL)
|
|
|
|
: false,
|
2020-04-13 22:38:46 +02:00
|
|
|
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,
|
2020-09-25 21:34:27 +02:00
|
|
|
adminAuthentication: process.env.ADMIN_AUTHENTICATION || 'unsecure',
|
2019-05-22 09:29:56 +02:00
|
|
|
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,
|
2020-06-17 08:03:02 +02:00
|
|
|
start: true,
|
2020-07-13 21:40:22 +02:00
|
|
|
keepAliveTimeout: 60 * 1000,
|
|
|
|
headersTimeout: 61 * 1000,
|
2020-08-03 13:34:10 +02:00
|
|
|
version,
|
2020-09-01 21:19:46 +02:00
|
|
|
enableHelmet: process.env.ENABLE_HELMET || false,
|
2019-05-22 09:29:56 +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-14 22:29:11 +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
|
|
|
},
|
|
|
|
};
|