Skip to content

Commit

Permalink
Provide different credentials when running in cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Butz committed Dec 9, 2019
1 parent 4d6a3f5 commit 713bf75
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ const express = require('express');
const redis = require('redis');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const indexRouter = require('./routes/index');

const services = isCloud() ?
require('./services.cloud') :
require('./services.local');

const app = express();
const redisClient = redis.createClient();
const redisClient = redis.createClient(services.REDIS_OPTIONS);

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use('/', indexRouter(process.env.NASA_API_KEY, redisClient));
app.use('/', indexRouter(services.NASA_API_KEY, redisClient));

module.exports = app;

function isCloud() {
return process.env.VCAP_SERVICES !== undefined;
}
26 changes: 26 additions & 0 deletions services.cloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const services = {
get NASA_API_KEY() {
const credentials = getVCAPServices()['credhub'][0]['credentials'];
return credentials['NASA_API_KEY'];
},

get REDIS_OPTIONS() {
const redisCredentials = getVCAPServices()['p.redis'][0]["credentials"];

const host = redisCredentials["host"];
const port = redisCredentials["port"];
const password = redisCredentials["password"];

return {
host,
port,
password
}
}
};

function getVCAPServices() {
return JSON.parse(process.env.VCAP_SERVICES);
}

module.exports = services;
17 changes: 17 additions & 0 deletions services.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const services = {
get NASA_API_KEY() {
return process.env.NASA_API_KEY;
},

get REDIS_OPTIONS() {
const host = '127.0.0.1';
const port = 6379;

return {
host,
port
}
}
};

module.exports = services;

0 comments on commit 713bf75

Please sign in to comment.