Refactor Redis into middleware
This commit is contained in:
parent
0fea2d7158
commit
8474cca94c
4 changed files with 45 additions and 75 deletions
29
lib/redis.js
Normal file
29
lib/redis.js
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue