From 6e48e629a491de7b452f30f8fed62130bd636873 Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Sun, 16 Oct 2016 12:00:44 -0700 Subject: [PATCH] more refactoring and renaming to make storage generic --- env.js | 18 +++++++++--------- lib/bootevent.js | 6 +++--- lib/entries.js | 2 +- lib/{storage.js => storage/mongo-storage.js} | 4 ++-- lib/websocket.js | 2 +- testing/populate.js | 4 ++-- .../{storage.test.js => mongo-storage.test.js} | 12 ++++++------ tests/mqtt.test.js | 4 ++-- 8 files changed, 26 insertions(+), 26 deletions(-) rename lib/{storage.js => storage/mongo-storage.js} (94%) rename tests/{storage.test.js => mongo-storage.test.js} (76%) diff --git a/env.js b/env.js index 750475d7cb4..4c9f5f46f6c 100644 --- a/env.js +++ b/env.js @@ -27,7 +27,7 @@ function config ( ) { setSSL(); setAPISecret(); setVersion(); - setMongo(); + setStorage(); updateSettings(); return env; @@ -90,12 +90,12 @@ function setVersion() { env.name = software.name; } -function setMongo() { - env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI'); - env.mongo_collection = readENV('MONGO_COLLECTION', 'entries'); +function setStorage() { + env.storageURI = readENV('STORAGE_URI') || readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI'); + env.entries_collection = readENV('ENTRIES_COLLECTION') || readENV('MONGO_COLLECTION', 'entries'); env.MQTT_MONITOR = readENV('MQTT_MONITOR', null); if (env.MQTT_MONITOR) { - var hostDbCollection = [env.mongo.split('mongodb://').pop().split('@').pop(), env.mongo_collection].join('/'); + var hostDbCollection = [env.storageURI.split('mongodb://').pop().split('@').pop(), env.entries_collection].join('/'); var mongoHash = crypto.createHash('sha1'); mongoHash.update(hostDbCollection); //some MQTT servers only allow the client id to be 23 chars @@ -117,10 +117,10 @@ function setMongo() { // Some people prefer to use a json configuration file instead. // This allows a provided json config to override environment variables var DB = require('./database_configuration.json'), - DB_URL = DB.url ? DB.url : env.mongo, - DB_COLLECTION = DB.collection ? DB.collection : env.mongo_collection; - env.mongo = DB_URL; - env.mongo_collection = DB_COLLECTION; + DB_URL = DB.url ? DB.url : env.storageURI, + DB_COLLECTION = DB.collection ? DB.collection : env.entries_collection; + env.storageURI = DB_URL; + env.entries_collection = DB_COLLECTION; } function updateSettings() { diff --git a/lib/bootevent.js b/lib/bootevent.js index 2cd706ae55d..04edfd775d8 100644 --- a/lib/bootevent.js +++ b/lib/bootevent.js @@ -18,14 +18,14 @@ function boot (env) { return ctx.bootErrors && ctx.bootErrors.length > 0; } - function setupMongo (ctx, next) { + function setupStorage (ctx, next) { if (hasBootErrors(ctx)) { return next(); } try { - require('./storage')(env, function ready ( err, store ) { + require('./storage/mongo-storage')(env, function ready ( err, store ) { // FIXME, error is always null, if there is an error, the storage.js will throw an exception console.log('Storage system ready'); ctx.store = store; @@ -170,7 +170,7 @@ function boot (env) { return require('bootevent')( ) .acquire(checkEnv) - .acquire(setupMongo) + .acquire(setupStorage) .acquire(setupAuthorization) .acquire(setupInternals) .acquire(ensureIndexes) diff --git a/lib/entries.js b/lib/entries.js index a4697e4a2ee..822a83355aa 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -116,7 +116,7 @@ function storage(env, ctx) { function api ( ) { // obtain handle usable for querying the collection associated // with these records - return ctx.store.db.collection(env.mongo_collection); + return ctx.store.db.collection(env.entries_collection); } // Expose all the useful functions diff --git a/lib/storage.js b/lib/storage/mongo-storage.js similarity index 94% rename from lib/storage.js rename to lib/storage/mongo-storage.js index 87f7c8d7b1c..d0531094a68 100644 --- a/lib/storage.js +++ b/lib/storage/mongo-storage.js @@ -19,7 +19,7 @@ function init (env, cb, forceNewConnection) { cb(null, mongo); } } else { - if (!env.mongo) { + if (!env.storageURI) { throw new Error('MongoDB connection string is missing'); } @@ -28,7 +28,7 @@ function init (env, cb, forceNewConnection) { var options = { replset: { socketOptions: { connectTimeoutMS : timeout, socketTimeoutMS : timeout }}}; var connect_with_retry = function(i) { - return MongoClient.connect(env.mongo, options, function connected(err, db) { + return MongoClient.connect(env.storageURI, options, function connected(err, db) { if (err) { if (i>20) { // Abort after retrying for more than 10 minutes diff --git a/lib/websocket.js b/lib/websocket.js index d4520379709..8e60825f033 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -27,7 +27,7 @@ function init (env, ctx, server) { // TODO: this would be better to have somehow integrated/improved var supportedCollections = { 'treatments' : env.treatments_collection, - 'entries': env.mongo_collection, + 'entries': env.entries_collection, 'devicestatus': env.devicestatus_collection, 'profile': env.profile_collection, 'food': env.food_collection diff --git a/testing/populate.js b/testing/populate.js index 61bbadf0342..34c307b7759 100644 --- a/testing/populate.js +++ b/testing/populate.js @@ -9,7 +9,7 @@ main(); function main() { var MongoClient = mongodb.MongoClient; - MongoClient.connect(env.mongo, function connected(err, db) { + MongoClient.connect(env.storageURI, function connected(err, db) { console.log('Connecting to mongo...'); if (err) { @@ -21,7 +21,7 @@ function main() { } function populate_collection(db) { - var cgm_collection = db.collection(env.mongo_collection); + var cgm_collection = db.collection(env.entries_collection); var new_cgm_record = util.get_cgm_record(); cgm_collection.insert(new_cgm_record, function (err) { diff --git a/tests/storage.test.js b/tests/mongo-storage.test.js similarity index 76% rename from tests/storage.test.js rename to tests/mongo-storage.test.js index 6f399746bdc..f743af7e2c9 100644 --- a/tests/storage.test.js +++ b/tests/mongo-storage.test.js @@ -12,12 +12,12 @@ describe('STORAGE', function () { }); it('The storage class should be OK.', function (done) { - should.exist(require('../lib/storage')); + should.exist(require('../lib/storage/mongo-storage')); done(); }); it('After initializing the storage class it should re-use the open connection', function (done) { - var store = require('../lib/storage'); + var store = require('../lib/storage/mongo-storage'); store(env, function (err1, db1) { should.not.exist(err1); @@ -31,11 +31,11 @@ describe('STORAGE', function () { }); it('When no connection-string is given the storage-class should throw an error.', function (done) { - delete env.mongo; - should.not.exist(env.mongo); + delete env.storageURI; + should.not.exist(env.storageURI); (function () { - return require('../lib/storage')(env, false, true); + return require('../lib/storage/mongo-storage')(env, false, true); }).should.throw('MongoDB connection string is missing'); done(); @@ -45,7 +45,7 @@ describe('STORAGE', function () { env.mongo = 'This is not a MongoDB connection-string'; (function () { - return require('../lib/storage')(env, false, true); + return require('../lib/storage/mongo-storage')(env, false, true); }).should.throw(Error); done(); diff --git a/tests/mqtt.test.js b/tests/mqtt.test.js index afbe9205ced..25d2a60614b 100644 --- a/tests/mqtt.test.js +++ b/tests/mqtt.test.js @@ -10,8 +10,8 @@ describe('mqtt', function ( ) { before(function () { process.env.MQTT_MONITOR = 'mqtt://user:password@localhost:12345'; - process.env.MONGO='mongodb://localhost/test_db'; - process.env.MONGO_COLLECTION='test_sgvs'; + process.env.STORAGE_URI='mongodb://localhost/test_db'; + process.env.ENTRIES_COLLECTION='test_sgvs'; self.env = require('../env')(); self.es = require('event-stream'); self.results = self.es.through(function (ch) { this.push(ch); });