From 747f2c63fe2f740c0d5c1b5aef466f7ad68ab77c Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 12:19:26 -0600 Subject: [PATCH 01/13] Referenced all keys and urls using environment variables. --- .gitignore | 2 ++ README | 13 ++++++++++++- app.js | 41 +++++++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a56a7ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules + diff --git a/README b/README index d1158b7..23ed74b 100644 --- a/README +++ b/README @@ -1,3 +1,14 @@ A Collaborative Rdio Jukebox -twtbox.com \ No newline at end of file +twtbox.com + +##Installation + +heroku create +heroku addons:add redistogo:nano +heroku config:add REDISTOGO_URL=redis://redistogo:password@redistogo.com:8000/ +heroku config:add HEROKU_URL=http://appname.herokuapp.com +heroku config:add RDIO_API_KEY=xxx +heroku config:add RDIO_API_SECRET=xxx +git push heroku master +heroku open \ No newline at end of file diff --git a/app.js b/app.js index 8456db2..5da533e 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,13 @@ +# Description: +# None +# +# Configuration: +# REDISTOGO_URL +# HEROKU_URL +# RDIO_API_KEY +# RDIO_API_SECRET +# + var express = require('express'); var sys = require('sys'); var io = require('socket.io'); @@ -7,32 +17,27 @@ var RedisStore = require('./my-connect-redis'); var app = module.exports = express.createServer(express.logger()); -var domain, mp_client, redisHost, redisPort, redisPass; +var domain, mp_client, redisHost, redisPort, redisPass, rclient; + +var Redis = require('redis'); +var Url = require('url'); + app.configure('development', function(){ - redisHost = "filefish.redistogo.com"; - redisPort = 9734; - redisPass = ""; // TODO: add the redis config. Removed for security - domain = "http://localhost:3000"; + info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); + rclient = Redis.createClient(info.port, info.hostname); + if(info.auth) { + rclient.auth(info.auth.split(":")[1]); + } + domain = process.env.HEROKU_URL || "http://localhost:3000"; mp_client = new mixpanel.Client("8c587841d6590b8d46ca00197d8339a0"); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function(){ - redisHost = "carp.redistogo.com"; - redisPort = 9069; - redisPass = ""; // TODO: add the redis config. Removed for security - domain = "http://twtbox.com"; mp_client = new mixpanel.Client("f5b01baad731fa1f37a2fd7be9a1de44"); app.use(express.errorHandler()); }); -var redis = require('redis'); -var rclient = redis.createClient(redisPort, redisHost); -var dbAuth = function() { rclient.auth(redisPass); } -rclient.addListener('connected', dbAuth); -rclient.addListener('reconnected', dbAuth); -dbAuth(); - app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); @@ -40,7 +45,7 @@ app.configure(function(){ app.use(express.methodOverride()); app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); app.use(express.cookieParser()); - app.use(express.session({ store: new RedisStore({port: redisPort, host: redisHost, password: redisPass}), secret: '' })); // TODO: Add secret, removed for security + app.use(express.session({ store: new RedisStore({client: rclient}), secret: 'test' })); // TODO: Add secret, removed for security app.use(app.router); app.use(express.static(__dirname + '/public')); }); @@ -52,7 +57,7 @@ app.dynamicHelpers({ var socket = io.listen(app); var OAuth = require('./oauth').OAuth; var oa = new OAuth("http://api.rdio.com/oauth/request_token", "http://api.rdio.com/oauth/access_token", - "", "", // TODO: Add the rdio oauth tokens. Removed for security + process.env.RDIO_API_KEY, process.env.RDIO_API_SECRET, // TODO: Add the rdio oauth tokens. Removed for security "1.0", domain + "/callback", "HMAC-SHA1"); var rdioEndpoint = "http://api.rdio.com/1/"; From 9dd1b614211f9faee0db99ad6eaa976f356ff6af Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:16:43 -0600 Subject: [PATCH 02/13] Standardized config for both dev and prod --- app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 5da533e..40dafeb 100644 --- a/app.js +++ b/app.js @@ -22,13 +22,16 @@ var domain, mp_client, redisHost, redisPort, redisPass, rclient; var Redis = require('redis'); var Url = require('url'); -app.configure('development', function(){ +app.configure(function(){ info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); rclient = Redis.createClient(info.port, info.hostname); if(info.auth) { rclient.auth(info.auth.split(":")[1]); } domain = process.env.HEROKU_URL || "http://localhost:3000"; +}); + +app.configure('development', function(){ mp_client = new mixpanel.Client("8c587841d6590b8d46ca00197d8339a0"); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); From 9cc6f7f3ed641b3c75cfdf1221800cedbb68d50a Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:19:31 -0600 Subject: [PATCH 03/13] It's nice when I remember how to comment in javascript --- app.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 40dafeb..306cc24 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,12 @@ -# Description: -# None -# -# Configuration: -# REDISTOGO_URL -# HEROKU_URL -# RDIO_API_KEY -# RDIO_API_SECRET -# +// Description: +// None +// +// Configuration: +// REDISTOGO_URL +// HEROKU_URL +// RDIO_API_KEY +// RDIO_API_SECRET +// var express = require('express'); var sys = require('sys'); From 370bf454aa62632796ab8ae7b3afb7dd3c11888a Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:25:49 -0600 Subject: [PATCH 04/13] Logging environment variable for redistogo_url --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index 306cc24..6bf2f80 100644 --- a/app.js +++ b/app.js @@ -24,6 +24,7 @@ var Url = require('url'); app.configure(function(){ info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); + console.log(process.env.REDISTOGO_URL); rclient = Redis.createClient(info.port, info.hostname); if(info.auth) { rclient.auth(info.auth.split(":")[1]); From 47d5a9658275565635a97f9d121e2550d2316801 Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:33:07 -0600 Subject: [PATCH 05/13] Added better logging for environment variable --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 6bf2f80..a66d3cd 100644 --- a/app.js +++ b/app.js @@ -24,7 +24,7 @@ var Url = require('url'); app.configure(function(){ info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); - console.log(process.env.REDISTOGO_URL); + console.log(info); rclient = Redis.createClient(info.port, info.hostname); if(info.auth) { rclient.auth(info.auth.split(":")[1]); From d44bb68985ad1d1601489fef734840009e183c1c Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:36:45 -0600 Subject: [PATCH 06/13] Took some of the config out of the config section --- app.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/app.js b/app.js index a66d3cd..4745020 100644 --- a/app.js +++ b/app.js @@ -22,15 +22,12 @@ var domain, mp_client, redisHost, redisPort, redisPass, rclient; var Redis = require('redis'); var Url = require('url'); -app.configure(function(){ info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); - console.log(info); rclient = Redis.createClient(info.port, info.hostname); if(info.auth) { rclient.auth(info.auth.split(":")[1]); } domain = process.env.HEROKU_URL || "http://localhost:3000"; -}); app.configure('development', function(){ mp_client = new mixpanel.Client("8c587841d6590b8d46ca00197d8339a0"); From a76640e8fd6a1e8c854cb04f49c92c7203f227eb Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 13:46:00 -0600 Subject: [PATCH 07/13] Switched back to generic connect-redis --- app.js | 2 +- my-connect-redis.js | 120 -------------------------------------------- package.json | 3 +- 3 files changed, 3 insertions(+), 122 deletions(-) delete mode 100644 my-connect-redis.js diff --git a/app.js b/app.js index 4745020..9766b35 100644 --- a/app.js +++ b/app.js @@ -13,7 +13,7 @@ var sys = require('sys'); var io = require('socket.io'); var mixpanel = require('mixpanel'); var _ = require('underscore'); -var RedisStore = require('./my-connect-redis'); +var RedisStore = require('connect-redis')(express); var app = module.exports = express.createServer(express.logger()); diff --git a/my-connect-redis.js b/my-connect-redis.js deleted file mode 100644 index 8e3217b..0000000 --- a/my-connect-redis.js +++ /dev/null @@ -1,120 +0,0 @@ - -/*! - * Connect - Redis - * Copyright(c) 2010 TJ Holowaychuk - * MIT Licensed - */ - -/** - * Module dependencies. - */ - -var Store = require('connect').session.Store - , redis = require('redis'); - -/** - * One day in seconds. - */ - -var oneDay = 86400; - -/** - * Initialize RedisStore with the given `options`. - * - * @param {Object} options - * @api public - */ - -var RedisStore = module.exports = function RedisStore(options) { - options = options || {}; - Store.call(this, options); - - this.client = redis.createClient(options.port, options.host); - var dbAuth = function(client) { client.auth(options.password); } - this.client.addListener('connected', dbAuth); - this.client.addListener('reconnected', dbAuth); - dbAuth(this.client); - -}; - -/** - * Inherit from `Store`. - */ - -RedisStore.prototype.__proto__ = Store.prototype; - -/** - * Attempt to fetch session by the given `sid`. - * - * @param {String} sid - * @param {Function} fn - * @api public - */ - -RedisStore.prototype.get = function(sid, fn){ - this.client.get(sid, function(err, data){ - try { - if (!data) return fn(); - fn(null, JSON.parse(data.toString())); - } catch (err) { - fn(err); - } - }); -}; - -/** - * Commit the given `sess` object associated with the given `sid`. - * - * @param {String} sid - * @param {Session} sess - * @param {Function} fn - * @api public - */ - -RedisStore.prototype.set = function(sid, sess, fn){ - try { - var maxAge = sess.cookie.maxAge - , ttl = 'number' == typeof maxAge - ? maxAge / 1000 | 0 - : oneDay - , sess = JSON.stringify(sess); - this.client.setex(sid, ttl, sess, function(){ - fn && fn.apply(this, arguments); - }); - } catch (err) { - fn && fn(err); - } -}; - -/** - * Destroy the session associated with the given `sid`. - * - * @param {String} sid - * @api public - */ - -RedisStore.prototype.destroy = function(sid, fn){ - this.client.del(sid, fn); -}; - -/** - * Fetch number of sessions. - * - * @param {Function} fn - * @api public - */ - -RedisStore.prototype.length = function(fn){ - this.client.dbsize(fn); -}; - -/** - * Clear all sessions. - * - * @param {Function} fn - * @api public - */ - -RedisStore.prototype.clear = function(fn){ - this.client.flushdb(fn); -}; \ No newline at end of file diff --git a/package.json b/package.json index 3b0bb94..108084b 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "redis":"0.5.7", "socket.io":"0.9.0", "mixpanel":"0.0.3", - "underscore":"1.1.6" + "underscore":"1.1.6", + "connect-redis":"1.4.0" } } From 6f5126f428b46f2e1cae18f4fbb32cc37992536e Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 14:14:41 -0600 Subject: [PATCH 08/13] Changing domain for playback --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 9766b35..46e6401 100644 --- a/app.js +++ b/app.js @@ -17,7 +17,7 @@ var RedisStore = require('connect-redis')(express); var app = module.exports = express.createServer(express.logger()); -var domain, mp_client, redisHost, redisPort, redisPass, rclient; +var domain, mp_client, hostname, redisHost, redisPort, redisPass, rclient; var Redis = require('redis'); var Url = require('url'); @@ -28,6 +28,7 @@ var Url = require('url'); rclient.auth(info.auth.split(":")[1]); } domain = process.env.HEROKU_URL || "http://localhost:3000"; + hostname = Url.parse(domain).hostname; app.configure('development', function(){ mp_client = new mixpanel.Client("8c587841d6590b8d46ca00197d8339a0"); @@ -154,7 +155,7 @@ app.get("/topsongs.json", adminRequired, function(req, res, next) { // admin view of room app.get('/r/:room', adminRequired, function(req, res, next) { oa.post(rdioEndpoint, req.session.oauth_access_token, req.session.oauth_access_token_secret, - { "method" : "getPlaybackToken", "domain" : "twtbox.com" }, function (error, data) { + { "method" : "getPlaybackToken", "domain" : "infinite-sierra-8002.herokuapp.com" }, function (error, data) { var playbackToken = domain == "http://localhost:3000" ? "GAlNi78J_____zlyYWs5ZG02N2pkaHlhcWsyOWJtYjkyN2xvY2FsaG9zdEbwl7EHvbylWSWFWYMZwfc=" : JSON.parse(data)["result"]; var renderRoom = function(res, song, offset, playbackToken, domain) { From bbfaf1645821772505afea7a5778c201c1145d99 Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 14:20:18 -0600 Subject: [PATCH 09/13] Determining playback hostname based off of env config --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 46e6401..8b6f1d8 100644 --- a/app.js +++ b/app.js @@ -155,7 +155,7 @@ app.get("/topsongs.json", adminRequired, function(req, res, next) { // admin view of room app.get('/r/:room', adminRequired, function(req, res, next) { oa.post(rdioEndpoint, req.session.oauth_access_token, req.session.oauth_access_token_secret, - { "method" : "getPlaybackToken", "domain" : "infinite-sierra-8002.herokuapp.com" }, function (error, data) { + { "method" : "getPlaybackToken", "domain" : hostname }, function (error, data) { var playbackToken = domain == "http://localhost:3000" ? "GAlNi78J_____zlyYWs5ZG02N2pkaHlhcWsyOWJtYjkyN2xvY2FsaG9zdEbwl7EHvbylWSWFWYMZwfc=" : JSON.parse(data)["result"]; var renderRoom = function(res, song, offset, playbackToken, domain) { From 77b09f740cbaf52cf8c58e92d8bfcab611303c76 Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Wed, 18 Jul 2012 14:20:30 -0600 Subject: [PATCH 10/13] Adding app configure block back in --- app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.js b/app.js index 8b6f1d8..d2f76b3 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,7 @@ var domain, mp_client, hostname, redisHost, redisPort, redisPass, rclient; var Redis = require('redis'); var Url = require('url'); +app.configure(function(){ info = Url.parse(process.env.REDISTOGO_URL || 'redis://localhost:6379'); rclient = Redis.createClient(info.port, info.hostname); if(info.auth) { @@ -29,6 +30,8 @@ var Url = require('url'); } domain = process.env.HEROKU_URL || "http://localhost:3000"; hostname = Url.parse(domain).hostname; + console.log(hostname); +}); app.configure('development', function(){ mp_client = new mixpanel.Client("8c587841d6590b8d46ca00197d8339a0"); From aac767ca62afd57bb15fafac03672b5842f2dbbf Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Thu, 19 Jul 2012 08:12:22 -0600 Subject: [PATCH 11/13] Moved session secret into environment var --- README | 1 + app.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README b/README index 23ed74b..2cad7df 100644 --- a/README +++ b/README @@ -10,5 +10,6 @@ heroku config:add REDISTOGO_URL=redis://redistogo:password@redistogo.com:8000/ heroku config:add HEROKU_URL=http://appname.herokuapp.com heroku config:add RDIO_API_KEY=xxx heroku config:add RDIO_API_SECRET=xxx +heroku config:add SESSION_SECRET=random_secret_string git push heroku master heroku open \ No newline at end of file diff --git a/app.js b/app.js index d2f76b3..79b3e9c 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ // HEROKU_URL // RDIO_API_KEY // RDIO_API_SECRET +// SESSION_SECRET // var express = require('express'); @@ -50,7 +51,7 @@ app.configure(function(){ app.use(express.methodOverride()); app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); app.use(express.cookieParser()); - app.use(express.session({ store: new RedisStore({client: rclient}), secret: 'test' })); // TODO: Add secret, removed for security + app.use(express.session({ store: new RedisStore({client: rclient}), secret: process.env.SESSION_SECRET })); // TODO: Add secret, removed for security app.use(app.router); app.use(express.static(__dirname + '/public')); }); From 86c72b2df57f909008375d358192165a2f837e66 Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Thu, 19 Jul 2012 08:14:21 -0600 Subject: [PATCH 12/13] Removed TODO comments, got rid of debug logging and removed vars that were no longer needed --- app.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app.js b/app.js index 79b3e9c..f1a2ab9 100644 --- a/app.js +++ b/app.js @@ -18,7 +18,7 @@ var RedisStore = require('connect-redis')(express); var app = module.exports = express.createServer(express.logger()); -var domain, mp_client, hostname, redisHost, redisPort, redisPass, rclient; +var domain, mp_client, hostname, rclient; var Redis = require('redis'); var Url = require('url'); @@ -31,7 +31,6 @@ app.configure(function(){ } domain = process.env.HEROKU_URL || "http://localhost:3000"; hostname = Url.parse(domain).hostname; - console.log(hostname); }); app.configure('development', function(){ From 150df45b79acb5318cc3cf65f655610e60082f84 Mon Sep 17 00:00:00 2001 From: Michael McClenaghan Date: Thu, 19 Jul 2012 08:15:50 -0600 Subject: [PATCH 13/13] Damn...missed a few changes in last commit --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index f1a2ab9..da20a1f 100644 --- a/app.js +++ b/app.js @@ -50,7 +50,7 @@ app.configure(function(){ app.use(express.methodOverride()); app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); app.use(express.cookieParser()); - app.use(express.session({ store: new RedisStore({client: rclient}), secret: process.env.SESSION_SECRET })); // TODO: Add secret, removed for security + app.use(express.session({ store: new RedisStore({client: rclient}), secret: process.env.SESSION_SECRET })); app.use(app.router); app.use(express.static(__dirname + '/public')); }); @@ -62,7 +62,7 @@ app.dynamicHelpers({ var socket = io.listen(app); var OAuth = require('./oauth').OAuth; var oa = new OAuth("http://api.rdio.com/oauth/request_token", "http://api.rdio.com/oauth/access_token", - process.env.RDIO_API_KEY, process.env.RDIO_API_SECRET, // TODO: Add the rdio oauth tokens. Removed for security + process.env.RDIO_API_KEY, process.env.RDIO_API_SECRET, "1.0", domain + "/callback", "HMAC-SHA1"); var rdioEndpoint = "http://api.rdio.com/1/";