mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
more refactoring and renaming to make storage generic
This commit is contained in:
@@ -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() {
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
+1
-1
@@ -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
|
||||
|
||||
+2
-2
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
+2
-2
@@ -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); });
|
||||
|
||||
Reference in New Issue
Block a user