Refactor mongodb connection middleware

This commit is contained in:
Jonathan Cremin 2015-08-22 13:35:06 +01:00
parent 51968374dd
commit 0fea2d7158
4 changed files with 45 additions and 65 deletions

View file

@ -1,5 +1,6 @@
import koa from 'koa';
import route from 'koa-route';
import stats from 'koa-statsd';
import websockify from 'koa-websocket';
import logger from 'koa-logger';
import compress from 'koa-compress';
@ -10,12 +11,11 @@ import redis from 'redis-url';
import coRedis from 'co-redis';
import raven from 'raven';
import auth from './lib/auth';
import mongoConnect from '../config/mongo';
import mongo from '../lib/mongo';
import * as user from './routes/user';
import * as file from './routes/file';
import debugname from 'debug';
const debug = debugname('hostr-api');
import stats from 'koa-statsd';
import StatsD from 'statsy';
if (process.env.SENTRY_DSN) {
@ -62,26 +62,9 @@ co(function*() {
console.error(err);
});
let mongoConnecting = false;
const mongoDeferred = {};
mongoDeferred.promise = new Promise(function(resolve, reject) {
mongoDeferred.resolve = resolve;
mongoDeferred.reject = reject;
});
function* getMongo() {
if (!mongoConnecting) {
mongoConnecting = true;
const db = yield mongoConnect();
mongoDeferred.resolve(db);
return db;
} else {
return mongoDeferred.promise;
}
}
app.use(mongo());
function* setupConnections(next){
this.db = yield getMongo();
this.redis = coRedisConn;
yield next;
}

View file

@ -1,21 +0,0 @@
import mongodb from 'mongodb-promisified';
const MongoClient = mongodb().MongoClient;
import debugname from 'debug';
const debug = debugname('hostr-api:db');
const uristring = process.env.MONGO_URL || process.env.MONGOLAB_URI || 'mongodb://localhost:27017/hostr';
export default function*() {
debug('Connecting to Mongodb');
const client = yield MongoClient.connect(uristring);
debug('Successfully connected to Mongodb');
client.Users = client.collection('users');
client.Files = client.collection('files');
client.Transactions = client.collection('transactions');
client.Logins = client.collection('logins');
client.Remember = client.collection('remember');
client.Reset = client.collection('reset');
client.Remember.ensureIndex({'created': 1}, {expireAfterSeconds: 2592000});
client.Files.ensureIndex({'owner': 1, 'status': 1, 'time_added': -1});
return client;
}

37
lib/mongo.js Normal file
View file

@ -0,0 +1,37 @@
import mongodb from 'mongodb-promisified';
const MongoClient = mongodb().MongoClient;
import debugname from 'debug';
const debug = debugname('hostr:mongo');
const uristring = process.env.MONGO_URL || process.env.MONGOLAB_URI || 'mongodb://localhost:27017/hostr';
let configuredClient = new Promise(function (resolve, reject) {
debug('Connecting to Mongodb');
return MongoClient.connect(uristring).then((client) => {
debug('Successfully connected to Mongodb');
client.Users = client.collection('users');
client.Files = client.collection('files');
client.Transactions = client.collection('transactions');
client.Logins = client.collection('logins');
client.Remember = client.collection('remember');
client.Reset = client.collection('reset');
client.Remember.ensureIndex({'created': 1}, {expireAfterSeconds: 2592000});
client.Files.ensureIndex({'owner': 1, 'status': 1, 'time_added': -1});
return resolve(client);
}).catch((e) => {
reject(e)
});
}).catch((e) => {
debug(e);
});
export default function() {
return function* (next) {
try {
this.db = yield configuredClient;
} catch (e) {
debug(e);
}
yield next;
}
}

View file

@ -3,6 +3,7 @@ import koa from 'koa';
import csrf from 'koa-csrf';
import route from 'koa-route';
import views from 'koa-views';
import stats from 'koa-statsd';
import logger from 'koa-logger';
import favicon from 'koa-favicon';
import redisStore from 'koa-redis';
@ -14,19 +15,17 @@ 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 mongoConnect from '../config/mongo';
import mongo from '../lib/mongo';
import * as index from './routes/index';
import * as file from './routes/file';
import * as pro from './routes/pro';
import * as user from './routes/user';
import mongodb from 'mongodb-promisified';
const objectId = mongodb().ObjectId;
import debugname from 'debug';
const debug = debugname('hostr-web');
import stats from 'koa-statsd';
import StatsD from 'statsy';
if (process.env.SENTRY_DSN) {
const ravenClient = new raven.Client(process.env.SENTRY_DSN);
@ -76,24 +75,7 @@ co(function*() {
debug(err);
});
let mongoConnecting = false;
const mongoDeferred = {};
mongoDeferred.promise = new Promise(function(resolve, reject) {
mongoDeferred.resolve = resolve;
mongoDeferred.reject = reject;
});
function* getMongo() {
if (!mongoConnecting) {
mongoConnecting = true;
const db = yield mongoConnect();
mongoDeferred.resolve(db);
return db;
} else {
return mongoDeferred.promise;
}
}
app.use(mongo());
app.use(compress());
app.use(bodyparser());
app.use(favicon(path.join(__dirname, 'public/images/favicon.png')));
@ -104,7 +86,6 @@ app.use(views('views', {
}));
app.use(function* setupConnections(next){
this.db = yield getMongo();
this.redis = coRedisConn;
yield next;
});