mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
* * Simplified bundling to just one bundle * Removed cache invalidation token from bundling * Generate strong persistent random string on deploy to use for JWT signing * WIP: moving api-secret and JWT signing to a separate centralized security component * Moved some server components away from project root * Fix issues reported by linter * Ignore detect-object-injection everywhere but the client * Make admin message button red * Remove alarms for some security alerts on code * api_secret is now fully contained in the enclave
98 lines
2.8 KiB
JavaScript
98 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs')
|
|
, path = require('path')
|
|
, language = require('../../../lib/language')()
|
|
, apiRoot = require('../../../lib/api/root')
|
|
, http = require('http')
|
|
, https = require('https')
|
|
;
|
|
|
|
function configure () {
|
|
const self = { };
|
|
|
|
self.prepareEnv = function prepareEnv({ apiSecret, useHttps, authDefaultRoles, enable }) {
|
|
|
|
if (useHttps) {
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
}
|
|
else {
|
|
process.env.INSECURE_USE_HTTP = true;
|
|
}
|
|
process.env.API_SECRET = apiSecret;
|
|
|
|
process.env.HOSTNAME = 'localhost';
|
|
const env = require('../../../lib/server/env')();
|
|
|
|
if (useHttps) {
|
|
env.ssl = {
|
|
key: fs.readFileSync(path.join(__dirname, '../api3/localhost.key')),
|
|
cert: fs.readFileSync(path.join(__dirname, '../api3/localhost.crt'))
|
|
};
|
|
}
|
|
|
|
env.settings.authDefaultRoles = authDefaultRoles;
|
|
env.settings.enable = enable;
|
|
|
|
return env;
|
|
};
|
|
|
|
|
|
/*
|
|
* Create new web server instance for testing purposes
|
|
*/
|
|
self.create = function createHttpServer ({
|
|
apiSecret = 'this is my long pass phrase',
|
|
useHttps = true,
|
|
authDefaultRoles = '',
|
|
enable = ['careportal', 'api']
|
|
}) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
try {
|
|
let instance = { },
|
|
hasBooted = false
|
|
;
|
|
|
|
instance.env = self.prepareEnv({ apiSecret, useHttps, authDefaultRoles, enable });
|
|
|
|
self.wares = require('../../../lib/middleware/')(instance.env);
|
|
instance.app = require('express')();
|
|
instance.app.enable('api');
|
|
|
|
require('../../../lib/server/bootevent')(instance.env, language).boot(function booted (ctx) {
|
|
instance.ctx = ctx;
|
|
instance.ctx.ddata = require('../../../lib/data/ddata')();
|
|
instance.ctx.apiRootApp = apiRoot(instance.env, ctx);
|
|
|
|
instance.app.use('/api', instance.ctx.apiRootApp);
|
|
|
|
const transport = useHttps ? https : http;
|
|
|
|
instance.server = transport.createServer(instance.env.ssl || { }, instance.app).listen(0);
|
|
instance.env.PORT = instance.server.address().port;
|
|
|
|
instance.baseUrl = `${useHttps ? 'https' : 'http'}://${instance.env.HOSTNAME}:${instance.env.PORT}`;
|
|
|
|
console.log(`Started ${useHttps ? 'SSL' : 'HTTP'} instance on ${instance.baseUrl}`);
|
|
hasBooted = true;
|
|
resolve(instance);
|
|
});
|
|
|
|
setTimeout(function watchDog() {
|
|
if (!hasBooted)
|
|
reject('timeout');
|
|
}, 30000);
|
|
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
};
|
|
|
|
|
|
return self;
|
|
}
|
|
|
|
module.exports = configure(); |