From 71ee377aeaa43d7da37762f797d5173736a8dfde Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 31 Jan 2015 19:52:39 -0800 Subject: [PATCH] using redisStore configure redis store for session storage clean lint debug debug debug --- .gitignore | 2 ++ package.json | 4 +++- server/config/environment/development.js | 5 +++++ server/config/environment/production.js | 12 ++++++++++++ server/config/express.js | 24 ++++++++++++++++++------ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 9540f07..f0ef630 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ pids *.pid *.seed +# Redis dumps +dump.rdb # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/package.json b/package.json index 26aea15..df265a5 100644 --- a/package.json +++ b/package.json @@ -29,14 +29,15 @@ "body-parser": "^1.10.2", "bower": "^1.3.12", "cloudinary": "^1.1.1", + "connect-redis": "^2.2.0", "cookie-parser": "^1.3.3", "express": "^4.11.1", "express-jwt": "^1.0.0", "express-session": "^1.10.1", "gulp": "^3.8.10", "gulp-compass": "~2.0.3", - "gulp-coveralls": "^0.1.3", "gulp-concat": "^2.4.3", + "gulp-coveralls": "^0.1.3", "gulp-exit": "0.0.2", "gulp-image": "^0.5.3", "gulp-istanbul": "^0.5.0", @@ -63,6 +64,7 @@ "passport-facebook": "^1.0.3", "passport-local": "^1.0.0", "passport-twitter": "^1.0.2", + "redis": "^0.12.1", "run-sequence": "^1.0.2", "url-to-screenshot": "^0.6.0" }, diff --git a/server/config/environment/development.js b/server/config/environment/development.js index 8480a03..88a2354 100644 --- a/server/config/environment/development.js +++ b/server/config/environment/development.js @@ -8,5 +8,10 @@ module.exports = { uri: 'mongodb://localhost/archivr-dev' }, + redis: { + hostname: '127.0.0.1', + port: 6379 + }, + seedDB: true }; diff --git a/server/config/environment/production.js b/server/config/environment/production.js index c4dbdb8..7d92e07 100644 --- a/server/config/environment/production.js +++ b/server/config/environment/production.js @@ -2,6 +2,11 @@ // Production specific configuration // ================================= + +// Parse RedisCloud environment variable +var url = require('url'); +var redisURL = url.parse(process.env.REDISCLOUD_URL || ''); + module.exports = { // Server IP ip: process.env.OPENSHIFT_NODEJS_IP || @@ -19,5 +24,12 @@ module.exports = { process.env.MONGOHQ_URL || process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME || 'mongodb://localhost/archivr' + }, + + // Redis connection options + redis: { + hostname: redisURL.hostname || '127.0.0.1', + port: redisURL.port || 6379, + auth: redisURL.auth ? redisURL.auth.split(':')[1] : undefined } }; diff --git a/server/config/express.js b/server/config/express.js index c9144cd..a422970 100644 --- a/server/config/express.js +++ b/server/config/express.js @@ -3,6 +3,14 @@ var bodyParser = require('body-parser'); var morgan = require('morgan'); var config = require('./environment'); var session = require('express-session'); +var RedisStore = require('connect-redis')(session); +var redis = require('redis'); +var url = require('url'); + +// Configure Redis server +var redisClient = redis.createClient(config.redis.port, config.redis.hostname, + { no_ready_check: true }); // jshint ignore:line +if (config.redis.auth) redisClient.auth(config.redis.auth); module.exports = function expressConfig(app) { // standard POST request body parser @@ -14,6 +22,16 @@ module.exports = function expressConfig(app) { // HTTP request logger middleware app.use(morgan('combined')); + app.use(session({ + // Use RedisStore for session storage + store: new RedisStore({ + client: redisClient + }), + secret: config.expressSessionSecret, + resave: false, + saveUninitialized: true + })); + // set view directory app.set('views', __dirname + '/../views'); @@ -23,12 +41,6 @@ module.exports = function expressConfig(app) { // set static asset dir app.use(express.static(__dirname + '/../../client/app/dist')); - app.use(session({ - secret: config.expressSessionSecret, - resave: false, - saveUninitialized: true - })); - // dynamically set port if in production otherwise use port 3000 app.set('port', config.port); app.set('jwtTokenSecret', config.jwtTokenSecret);