-
Notifications
You must be signed in to change notification settings - Fork 3
/
env.js
80 lines (63 loc) · 2.75 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
// Loads the environment and makes it accessible,
// and also has sensible defaults
// == BSD2 LICENSE ==
// Copyright (c) 2014, Tidepool Project
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the associated License, which is identical to the BSD 2-Clause
// License as published by the Open Source Initiative at opensource.org.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the License for more details.
//
// You should have received a copy of the License along with this program; if
// not, you can obtain one from Tidepool Project at tidepool.org.
// == BSD2 LICENSE ==
'use strict';
var fs = require('fs');
var config = require('amoeba').config;
var cs = require('amoeba').mongoUtil.toConnectionString;
function maybeReplaceWithContentsOfFile(obj, field)
{
var potentialFile = obj[field];
if (potentialFile != null && fs.existsSync(potentialFile)) {
obj[field] = fs.readFileSync(potentialFile).toString();
}
}
module.exports = (function() {
var env = {};
// The port to attach an HTTP listener, if null, no HTTP listener will be attached
env.httpPort = config.fromEnvironment('PORT', null);
// The port to attach an HTTPS listener, if null, no HTTPS listener will be attached
env.httpsPort = config.fromEnvironment('HTTPS_PORT', null);
// The https config to pass along to https.createServer.
var theConfig = config.fromEnvironment('HTTPS_CONFIG', null);
env.httpsConfig = null;
if (theConfig != null) {
env.httpsConfig = JSON.parse(theConfig);
maybeReplaceWithContentsOfFile(env.httpsConfig, 'key');
maybeReplaceWithContentsOfFile(env.httpsConfig, 'cert');
maybeReplaceWithContentsOfFile(env.httpsConfig, 'pfx');
}
if (env.httpsPort != null && env.httpsConfig == null) {
throw new Error('No https config provided, please set HTTPS_CONFIG with at least the certificate to use.');
}
if (env.httpPort == null && env.httpsPort == null) {
throw new Error('Must specify either PORT or HTTPS_PORT in your environment.');
}
env.mongo = {
connectionString: cs('gatekeeper')
}
env.userApi = {
// Name of the service to authenticate user
userService: config.fromEnvironment('USERSERVICE', 'shoreline:9107'),
// Name of this server to pass to user-api when getting a server token
serverName: config.fromEnvironment('SERVER_NAME', 'gatekeeper'),
// The secret to use when getting a server token from user-api
serverSecret: config.fromEnvironment('SERVER_SECRET')
};
// The service name to publish on discovery
env.serviceName = config.fromEnvironment('SERVICE_NAME', 'gatekeeper');
return env;
})();