Skip to content

Commit

Permalink
Add support for binding style env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
sspeiche committed Aug 19, 2018
1 parent b078bcf commit 5f15c1e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@ var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
mongoURLLabel = "";

if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(),
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'],
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'],
mongoDatabase = process.env[mongoServiceName + '_DATABASE'],
mongoPassword = process.env[mongoServiceName + '_PASSWORD']
mongoUser = process.env[mongoServiceName + '_USER'];
if (mongoURL == null) {
var mongoHost, mongoPort, mongoDatabase, mongoPassword, mongoUser;
// If using plane old env vars via service discovery
if (process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase();
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'];
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'];
mongoDatabase = process.env[mongoServiceName + '_DATABASE'];
mongoPassword = process.env[mongoServiceName + '_PASSWORD'];
mongoUser = process.env[mongoServiceName + '_USER'];

// If using env vars from secret from service binding
} else if (process.env.database_name) {
mongoDatabase = process.env.database_name;
mongoPassword = process.env.password;
mongoUser = process.env.username;
var mongoUriParts = process.env.uri && process.env.uri.split("//");
if (mongoUriParts.length == 2) {
mongoUriParts = mongoUriParts[1].split(":");
if (mongoUriParts && mongoUriParts.length == 2) {
mongoHost = mongoUriParts[0];
mongoPort = mongoUriParts[1];
}
}
}

if (mongoHost && mongoPort && mongoDatabase) {
mongoURLLabel = mongoURL = 'mongodb://';
Expand All @@ -29,7 +47,6 @@ if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
// Provide UI label that excludes user id and pw
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;

}
}
var db = null,
Expand Down

0 comments on commit 5f15c1e

Please sign in to comment.