forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.js
96 lines (83 loc) · 3.85 KB
/
env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var env = { };
var crypto = require('crypto');
var consts = require('./lib/constants');
// Module to constrain all config and environment parsing to one spot.
function config ( ) {
/*
* First inspect a bunch of environment variables:
* * PORT - serve http on this port
* * MONGO_CONNECTION, CUSTOMCONNSTR_mongo - mongodb://... uri
* * CUSTOMCONNSTR_mongo_collection - name of mongo collection with "sgv" documents
* * CUSTOMCONNSTR_mongo_settings_collection - name of mongo collection to store configurable settings
* * API_SECRET - if defined, this passphrase is fed to a sha1 hash digest, the hex output is used to create a single-use token for API authorization
* * NIGHTSCOUT_STATIC_FILES - the "base directory" to use for serving
* static files over http. Default value is the included `static`
* directory.
*/
var software = require('./package.json');
var git = require('git-rev');
if (readENV('SCM_GIT_EMAIL') == 'windowsazure' && readENV('ScmType') == 'GitHub') {
git.cwd('/home/site/repository');
}
if (readENV('SCM_COMMIT_ID')) {
env.head = readENV('SCM_COMMIT_ID');
} else {
git.short(function record_git_head (head) {
console.log("GIT HEAD", head);
env.head = head;
});
}
env.version = software.version;
env.name = software.name;
env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
env.PORT = readENV('PORT', 1337);
env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI');
env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
env.settings_collection = readENV('MONGO_SETTINGS_COLLECTION', 'settings');
env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');
env.enable = readENV('ENABLE');
var shasum = crypto.createHash('sha1');
/////////////////////////////////////////////////////////////////
// A little ugly, but we don't want to read the secret into a var
/////////////////////////////////////////////////////////////////
var useSecret = (readENV('API_SECRET') && readENV('API_SECRET').length > 0);
env.api_secret = null;
// if a passphrase was provided, get the hex digest to mint a single token
if (useSecret) {
if (readENV('API_SECRET').length < consts.MIN_PASSPHRASE_LENGTH) {
var msg = ["API_SECRET should be at least", consts.MIN_PASSPHRASE_LENGTH, "characters"];
var err = new Error(msg.join(' '));
// console.error(err);
throw err;
process.exit(1);
}
shasum.update(readENV('API_SECRET'));
env.api_secret = shasum.digest('hex');
}
// For pushing notifications to Pushover.
env.pushover_api_token = readENV('PUSHOVER_API_TOKEN');
env.pushover_user_key = readENV('PUSHOVER_USER_KEY') || readENV('PUSHOVER_GROUP_KEY');
// TODO: clean up a bit
// 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,
DB_SETTINGS_COLLECTION = DB.settings_collection ? DB.settings_collection : env.settings_collection;
env.mongo = DB_URL;
env.mongo_collection = DB_COLLECTION;
env.settings_collection = DB_SETTINGS_COLLECTION;
env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');
return env;
}
function readENV(varName, defaultValue) {
//for some reason Azure uses this prefix, maybe there is a good reason
var value = process.env['CUSTOMCONNSTR_' + varName]
|| process.env['CUSTOMCONNSTR_' + varName.toLowerCase()]
|| process.env[varName]
|| process.env[varName.toLowerCase()];
return value || defaultValue;
}
module.exports = config;