Skip to content
This repository has been archived by the owner on Sep 13, 2019. It is now read-only.

Commit

Permalink
Load configs from file to config db
Browse files Browse the repository at this point in the history
Partially addresses HospitalRun/hospitalrun-frontend#153
and HospitalRun/hospitalrun-frontend/#631
  • Loading branch information
jkleinsc committed Mar 23, 2017
1 parent 5880a01 commit 7b8bdd6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"array-bracket-spacing": [2, "never"],
"block-scoped-var": 2,
Expand Down
53 changes: 53 additions & 0 deletions routes/10-couchproxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
var forward = require('../forward.js');

var CONFIGS_TO_LOAD = {
'config_disable_offline_sync': 'disableOfflineSync',
'config_use_google_auth': 'useGoogleAuth',
'config_log_metrics': 'logNetworkMetrics',
'config_external_search': 'searchURL'
};

module.exports = function(app, config) {
var nano = require('nano')(config.couchAuthDbURL);
var configDB = nano.use('config');
function loadConfigs() {
var configIds = Object.keys(CONFIGS_TO_LOAD);
configDB.fetch({keys: configIds}, (err, configValues) => {
if (err) {
console.log('Error getting configurations to update', err);
} else {
let configsToUpdate = [];
configValues.rows.forEach((configValue) => {
var matchingConfig = CONFIGS_TO_LOAD[configValue.key];
var valueFromConfigFile = config[matchingConfig];
if (!valueFromConfigFile) {
valueFromConfigFile = false;
}
if (configValue.key === 'config_external_search' &&
valueFromConfigFile && valueFromConfigFile !== '') {
valueFromConfigFile = true;
}
var dbConfigValue = '';
if (!configValue.error && configValue.doc) {
dbConfigValue = configValue.doc.value;
}
if (dbConfigValue !== valueFromConfigFile) {
let docToUpdate = configValue.doc;
if (!docToUpdate) {
docToUpdate = {
_id: configValue.key
};
}
docToUpdate.value = valueFromConfigFile;
configsToUpdate.push(docToUpdate);
}
});
if (configsToUpdate.length > 0) {
configDB.bulk({docs: configsToUpdate}, (err) => {
if (err) {
console.log('Error updating configs:', err);
}
});
}
}
});

}
loadConfigs();
app.use('/db/', forward(config.couchDbURL, config, true));
};

0 comments on commit 7b8bdd6

Please # to comment.