Permalink
Browse files

Step 2 - Works on local

  • Loading branch information...
ciberch
ciberch committed May 15, 2012
1 parent f3e9398 commit 58cf846f30d8aa6423104953f51b8ed02ee211a9
Showing with 30 additions and 18 deletions.
  1. +1 −1 server.js
  2. +29 −17 siteConfig.js
View
@@ -23,7 +23,7 @@ var DummyHelper = require('./lib/dummy-helper');
// Session store
var RedisStore = require('connect-redis')(express);
-var sessionStore = new RedisStore;
+var sessionStore = new RedisStore(siteConf.redisOptions);
@ciberch

ciberch May 18, 2012

Owner

Use the redis info from either localhost or Cloud Foundry

var app = module.exports = express.createServer();
app.listen(siteConf.port, null);
View
@@ -1,40 +1,52 @@
+var cf = require('cloudfoundry');
@ciberch

ciberch May 18, 2012

Owner

This is a module which helps us parse the environment variables set by Cloud Foundry at app runtime.

var settings = {
- 'sessionSecret': 'sessionSecret'
+ 'sessionSecret': 'sessionSecret-238273283abs'
+ , 'internal_host' : '127.0.0.1'
+ , 'internal_port' : 8080
@ciberch

ciberch May 18, 2012

Owner

Cloud Foundry uses ephemeral ports so to have the node.js app listen on the proper port we have to read it from the environment. When running locally these ports match the external ports

, 'port': 8080
- , 'uri': 'http://localhost:8080' // Without trailing /
-
- // You can add multiple recipiants for notifo notifications
+ , 'uri': 'http://moni-air.local:8080' // Without trailing /
@ciberch

ciberch May 18, 2012

Owner

When using Twitter Auth you cannot pass localhost as the callback host so I used my machine's name

+ , 'redisOptions': {host: '127.0.0.1', port: 6379}
@ciberch

ciberch May 18, 2012

Owner

This is the default port that Redis runs on locally. When running on Cloud Foundry we have to read it from an environment variable

, 'notifoAuth': null /*[
{
'username': ''
, 'secret': ''
}
]*/
- /*
+
// Enter API keys to enable auth services, remove entire object if they aren't used.
, 'external': {
'facebook': {
- appId: '123983866527489',
- appSecret: '6edf1327ege27bbba2e239f73cd866c4'
+ appId: process.env.facebook_app_id,
@ciberch

ciberch May 18, 2012

Owner

Instead of hardcoding the credentials here and not checking this file into GitHub we can just set and read environment variables.

+ appSecret: process.env.facebook_app_secret
}
@ciberch

ciberch May 18, 2012

Owner

This code will also work when the app is running on Cloud Foundry

, 'twitter': {
- consumerKey: 'eA54JQ6rtdZE7nqaRa6Oa',
- consumerSecret: '6u2makgFdf4F6EauP7osa54L34SouU6eLgaadTD435Rw'
+ consumerKey: process.env.twitter_consumer_key,
+ consumerSecret: process.env.twitter_consumer_secret
}
, 'github': {
- appId: '1444g6a7d26a3f716b47',
- appSecret: 'e84f13367f328da4b8c96a4f74gfe7e421b6a206'
+ appId: process.env.github_client_id,
+ appSecret: process.env.github_client_secret
}
}
- */
- , 'debug': (process.env.NODE_ENV !== 'production')
+
+ , 'debug': cf.cloud
@ciberch

ciberch May 18, 2012

Owner

boolean value returned by method in cloudfoundry module which says whether we are running on the Cloud or not

};
-if (process.env.NODE_ENV == 'production') {
- settings.uri = 'http://yourname.no.de';
- settings.port = process.env.PORT || 80; // Joyent SmartMachine uses process.env.PORT
+if (cf.cloud) {
+ settings.uri = 'http://' + cf.app.name + '.cloudfoundry.com';
+ settings.internal_host = cf.host;
+ settings.internal_port = cf.port;
@ciberch

ciberch May 18, 2012

Owner

cloudfoundry module allows us to read the app's environmental info including the internal host and port for node to listen on. If running multiple instances, each instance will get its own host and port

+ settings.port = 80; // CloudFoundry uses process.env.VMC_APP_PORT
@ciberch

ciberch May 18, 2012

Owner

This is the external port for socket.io

+
+ settings.airbrakeApiKey = process.env.airbrake_api_key; // Error logging, Get free API key from https://airbrakeapp.com/account/new/Free
- //settings.airbrakeApiKey = '0190e64f92da110c69673b244c862709'; // Error logging, Get free API key from https://airbrakeapp.com/account/new/Free
+ if (cf.redis['redis-asms'] != null) {
@ciberch

ciberch May 18, 2012

Owner

This code assumes we named our redis service bound to the app as redis-asms

+ var redisConfig = cf.redis['redis-asms'].credentials;
+ settings.redisOptions.port = redisConfig.port;
+ settings.redisOptions.host = redisConfig.hostname;
+ settings.redisOptions.pass = redisConfig.password;
+ }
+ settings.user_email = cf.app['users'][0];
}
module.exports = settings;

0 comments on commit 58cf846

Please sign in to comment.