Refactor Redis into middleware
This commit is contained in:
parent
0fea2d7158
commit
8474cca94c
4 changed files with 45 additions and 75 deletions
41
api/app.js
41
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;
|
||||
|
|
20
app.js
20
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));
|
||||
|
|
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;
|
||||
}
|
||||
}
|
30
web/app.js
30
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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue