Skip to content

Commit

Permalink
import many settings on boot from remote URL
Browse files Browse the repository at this point in the history
Allows sharing commonly used configurations amongst folks.
CC @jasoncalabrese.
  • Loading branch information
bewest committed Aug 7, 2016
1 parent 0860064 commit dd2d0ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function config ( ) {
*/
env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
env.PORT = readENV('PORT', 1337);
env.IMPORT_CONFIG = readENV('IMPORT_CONFIG', null);
env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');

if (env.err) {
Expand Down
29 changes: 29 additions & 0 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ function boot (env) {
return ctx.bootErrors && ctx.bootErrors.length > 0;
}

function augmentSettings (ctx, next) {
var configURL = env.IMPORT_CONFIG || null;
var url = require('url');
var href = null;
try {
href = url.parse(configURL).href;
} catch (e) {
}
if(configURL && href) {
var request = require('request');
console.log('Getting', href);
request.get({url: href, json: true}, function (err, resp, body) {
console.log("GOT", body);
if (err) {
console.log('Attempt to fetch config', href, 'failed.');
console.error(err);
throw err;
} else {
console.log('extending');
_.merge(env.settings, body);
}
next( );
});
} else {
next( );
}
}

function setupMongo (ctx, next) {

if (hasBootErrors(ctx)) {
Expand Down Expand Up @@ -170,6 +198,7 @@ function boot (env) {

return require('bootevent')( )
.acquire(checkEnv)
.acquire(augmentSettings)
.acquire(setupMongo)
.acquire(setupAuthorization)
.acquire(setupInternals)
Expand Down

0 comments on commit dd2d0ae

Please sign in to comment.