diff --git a/app-init.js b/app-init.js index be5627af..319fa0c3 100644 --- a/app-init.js +++ b/app-init.js @@ -39,12 +39,24 @@ const sessionStore = new MySQLStore({ */ -const mongoCredentials = { - host: process.env.MONGO_DB_HOST || 'localhost', - port: process.env.MONGO_DB_PORT || 27017, +function getMongoDbConnectionString () { + // Allow the connection string builder to be overridden by an environment variable + if (process.env.MONGO_DB_CONNECTION_STRING) { + return process.env.MONGO_DB_CONNECTION_STRING; + } + + const host = process.env.MONGO_DB_HOST || 'localhost'; + const port = process.env.MONGO_DB_PORT || 27017; + const user = process.env.MONGO_DB_USER || ''; + const password = process.env.MONGO_DB_PASSWORD || ''; + const authSource = process.env.MONGO_DB_AUTHSOURCE || ''; + + const useAuth = user && password; + + return `mongodb://${useAuth ? `${user}:${password}@` : ''}${host}:${port}/sessions${authSource ? `?authSource=${authSource}` : ''}`; } -const url = 'mongodb://'+ mongoCredentials.host +':'+mongoCredentials.port+'/sessions'; +const url = getMongoDbConnectionString(); const sessionStore = new MongoStore({ url: url,