From 8474cca94c9dc56c1b1323a17ae225f14f5c2264 Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sat, 22 Aug 2015 13:57:19 +0100 Subject: [PATCH] Refactor Redis into middleware --- api/app.js | 41 +++++++++++------------------------------ app.js | 20 ++------------------ lib/redis.js | 29 +++++++++++++++++++++++++++++ web/app.js | 30 +++--------------------------- 4 files changed, 45 insertions(+), 75 deletions(-) create mode 100644 lib/redis.js diff --git a/api/app.js b/api/app.js index 6db76f4..584feed 100644 --- a/api/app.js +++ b/api/app.js @@ -7,11 +7,10 @@ import compress from 'koa-compress'; import bodyparser from 'koa-bodyparser'; import cors from 'kcors'; import co from 'co'; -import redis from 'redis-url'; -import coRedis from 'co-redis'; import raven from 'raven'; import auth from './lib/auth'; import mongo from '../lib/mongo'; +import redis from '../lib/redis'; import * as user from './routes/user'; import * as file from './routes/file'; import debugname from 'debug'; @@ -25,7 +24,13 @@ if (process.env.SENTRY_DSN) { const app = websockify(koa()); -const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379'; +app.use(function* (next){ + this.set('Server', 'Nintendo 64'); + if(this.req.headers['x-forwarded-proto'] === 'http'){ + return this.redirect('https://' + this.req.headers.host + this.req.url); + } + yield next; +}); let statsdOpts = {prefix: 'hostr-api', host: process.env.STATSD_HOST || 'localhost'}; let statsd = new StatsD(statsdOpts); @@ -42,34 +47,10 @@ app.use(cors({ credentials: true })); -app.use(function* (next){ - this.set('Server', 'Nintendo 64'); - if(this.req.headers['x-forwarded-proto'] === 'http'){ - return this.redirect('https://' + this.req.headers.host + this.req.url); - } - yield next; -}); - -const redisConn = redis.connect(redisUrl); -let coRedisConn = {}; - -co(function*() { - coRedisConn = coRedis(redisConn); - coRedisConn.on('error', function (err) { - debug('Redis error ' + err); - }); -}).catch(function(err) { - console.error(err); -}); - app.use(mongo()); - -function* setupConnections(next){ - this.redis = coRedisConn; - yield next; -} -app.ws.use(setupConnections); -app.use(setupConnections); +app.use(redis()); +app.ws.use(mongo()); +app.ws.use(redis()); app.use(route.get('/', function* (){ this.status = 200; diff --git a/app.js b/app.js index a966b52..55d27ac 100644 --- a/app.js +++ b/app.js @@ -2,8 +2,7 @@ import koa from 'koa'; import mount from 'koa-mount'; import route from 'koa-route'; import websockify from 'koa-websocket'; -import redis from 'redis-url'; -import coRedis from 'co-redis'; +import redis from './lib/redis'; import co from 'co'; import api from './api/app'; import { events as fileEvents } from './api/routes/file'; @@ -20,22 +19,7 @@ const app = websockify(koa()); app.keys = [process.env.KEYS || 'INSECURE']; -const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379'; - -let coRedisConn = {}; - -co(function*() { - coRedisConn = coRedis(redis.connect(redisUrl)); - coRedisConn.on('error', function (err) { - debug('Redis error ' + err); - }); -}).catch(function(err) { - console.error(err); -}); -app.ws.use(function*(next) { - this.redis = coRedisConn; - yield next; -}); +app.ws.use(redis()); app.ws.use(route.all('/api/user', userEvents)); app.ws.use(route.all('/api/file/:id', fileEvents)); diff --git a/lib/redis.js b/lib/redis.js new file mode 100644 index 0000000..0ad3982 --- /dev/null +++ b/lib/redis.js @@ -0,0 +1,29 @@ +import redis from 'redis-url'; +import coRedis from 'co-redis'; +import debugname from 'debug'; +const debug = debugname('hostr:redis'); + +const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379'; + +let connection = new Promise((resolve, reject) => { + debug('Connecting to Redis'); + const client = redis.connect(redisUrl); + const wrapped = coRedis(client); + wrapped.client = client; + wrapped.on('error', (err) => { + debug('Client error: ' + err); + }); + wrapped.on('ready', () => { + debug('Successfully connected to Redis'); + resolve(wrapped); + }); +}).catch((err) => { + debug('Promise error: ' + err); +}); + +export default function () { + return function* redisMiddleware(next) { + this.redis = yield connection; + yield next; + } +} diff --git a/web/app.js b/web/app.js index d45e6a1..0b3cc39 100644 --- a/web/app.js +++ b/web/app.js @@ -12,13 +12,12 @@ import bodyparser from 'koa-bodyparser'; import session from 'koa-generic-session'; import staticHandler from 'koa-file-server'; import co from 'co'; -import redis from 'redis-url'; -import coRedis from 'co-redis'; import raven from 'raven'; import StatsD from 'statsy'; // waiting for PR to be merged, can remove swig dependency when done import errors from '../lib/koa-error'; import mongo from '../lib/mongo'; +import redis from '../lib/redis'; import * as index from './routes/index'; import * as file from './routes/file'; import * as pro from './routes/pro'; @@ -63,19 +62,8 @@ app.use(function*(next){ yield next; }); -const redisConn = redis.connect(redisUrl); -let coRedisConn = {}; - -co(function*() { - coRedisConn = coRedis(redisConn); - coRedisConn.on('error', function (err) { - debug('Redis error ' + err); - }); -}).catch(function(err) { - debug(err); -}); - app.use(mongo()); +app.use(redis()); app.use(compress()); app.use(bodyparser()); app.use(favicon(path.join(__dirname, 'public/images/favicon.png'))); @@ -85,23 +73,11 @@ app.use(views('views', { default: 'ejs' })); -app.use(function* setupConnections(next){ - this.redis = coRedisConn; - yield next; -}); - app.keys = [process.env.KEYS || 'INSECURE']; app.use(session({ - store: redisStore({client: redisConn}) + store: redisStore({client: redis().client}) })); -app.use(function* objectIdSession(next) { - if (this.session.user) { - this.session.user.id = objectId(this.session.user.id); - } - yield next; -}); - app.use(route.get('/', index.main)); app.use(route.get('/account', index.main)); app.use(route.get('/billing', index.main));