1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/options.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict';
2016-12-04 14:09:37 +01: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');
const { defaultLogProvider, validateLogProvider } = require('./logger');
const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
2019-05-22 09:29:56 +02:00
function defaultOptions() {
return {
databaseUrl: defaultDatabaseUrl(),
databaseSchema: 'public',
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,
enableLegacyRoutes: false,
2019-05-22 09:29:56 +02:00
extendedPermissions: false,
publicFolder,
enableRequestLogger: false,
2019-05-22 09:29:56 +02:00
sessionAge: THIRTY_DAYS,
adminAuthentication: 'unsecure',
ui: {},
importFile: undefined,
dropBeforeImport: false,
getLogger: defaultLogProvider,
customContextFields: [],
disableDBMigration: false,
2019-05-22 09:29:56 +02:00
};
}
function defaultDatabaseUrl() {
if (process.env.DATABASE_URL_FILE) {
return readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
} else if (process.env.DATABASE_URL) {
2019-05-22 09:29:56 +02:00
return process.env.DATABASE_URL;
} else {
return undefined;
}
}
module.exports = {
createOptions: (opts = {}) => {
const options = merge(defaultOptions(), opts);
2016-12-01 00:42:14 +01: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(
`Unleash requires database details to start. See https://unleash.github.io/docs/getting_started`
2017-06-28 10:17:14 +02:00
);
}
2019-03-12 10:46:08 +01:00
options.listen = options.pipe
? { path: options.pipe }
: { port: options.port, host: options.host };
validateLogProvider(options.getLogger);
return options;
2016-12-04 14:09:37 +01:00
},
};