2016-11-30 23:41:57 +01:00
'use strict' ;
2016-12-04 14:09:37 +01:00
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' ) ;
2019-06-14 18:10:38 +02:00
const fs = require ( 'fs' ) ;
2016-11-30 23:41:57 +01:00
2016-12-03 14:09:09 +01:00
const isDev = ( ) => process . env . NODE _ENV === 'development' ;
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' ,
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 : true ,
extendedPermissions : false ,
publicFolder ,
enableRequestLogger : isDev ( ) ,
secret : 'UNLEASH-SECRET' ,
sessionAge : THIRTY _DAYS ,
adminAuthentication : 'unsecure' ,
ui : { } ,
importFile : undefined ,
dropBeforeImport : false ,
getLogger : defaultLogProvider ,
} ;
}
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 ) {
return fs . 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 if ( isDev ( ) || process . env . DATABASE _HOST ) {
const dbUsername = process . env . DATABASE _USERNAME || 'unleash_user' ;
const dbPassword = process . env . DATABASE _PASSWORD || 'passord' ;
const dbHost = process . env . DATABASE _HOST || 'localhost' ;
const dbPort = process . env . DATABASE _PORT || 5432 ;
const dbName = process . env . DATABASE _NAME || 'unleash' ;
const sslSupport = process . env . DATABASE _SSL || 'true' ;
return ` postgres:// ${ dbUsername } : ${ dbPassword } @ ${ dbHost } : ${ dbPort } / ${ dbName } ?ssl= ${ sslSupport } ` ;
} else {
return undefined ;
}
2019-05-22 08:24:22 +02:00
}
2016-11-30 23:41:57 +01:00
module . exports = {
2017-06-28 10:17:14 +02:00
createOptions : opts => {
2019-05-22 09:29:56 +02:00
const options = Object . assign ( { } , defaultOptions ( ) , opts ) ;
2016-12-01 00:42:14 +01:00
2016-12-03 13:45:22 +01:00
if ( ! options . databaseUrl ) {
2017-06-28 10:17:14 +02:00
throw new Error (
2019-05-22 08:24:22 +02:00
'You must either pass databaseUrl option or set environemnt variable DATABASE_URL || (DATABASE_HOST, DATABASE_PORT, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME)'
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
} ,
} ;