2020-03-24 23:22:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const proxyPath = '/v1';
|
|
|
|
|
|
|
|
module.exports = function(app, options) {
|
|
|
|
// For options, see:
|
|
|
|
// https://github.com/nodejitsu/node-http-proxy
|
|
|
|
|
2020-04-10 21:13:04 +00:00
|
|
|
// This is probably not safe to do, but it works for now.
|
|
|
|
let cacheKey = `${options.project.configPath()}|${options.environment}`;
|
|
|
|
let config = options.project.configCache.get(cacheKey);
|
|
|
|
|
|
|
|
// Disable the proxy completely when Mirage is enabled. No requests to the API
|
|
|
|
// will be being made, and having the proxy attempt to connect to Nomad when it
|
|
|
|
// is not running can result in socket max connections that block the livereload
|
|
|
|
// server from reloading.
|
|
|
|
if (config['ember-cli-mirage'].enabled !== false) {
|
|
|
|
options.ui.writeInfoLine('Mirage is enabled. Not starting proxy');
|
|
|
|
delete options.proxy;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:12:20 +00:00
|
|
|
let proxyAddress = options.proxy;
|
|
|
|
|
2020-03-24 23:22:16 +00:00
|
|
|
let server = options.httpServer;
|
|
|
|
let proxy = require('http-proxy').createProxyServer({
|
2020-04-10 21:12:20 +00:00
|
|
|
target: proxyAddress,
|
2020-03-24 23:22:16 +00:00
|
|
|
ws: true,
|
2020-04-10 21:12:20 +00:00
|
|
|
changeOrigin: true,
|
2020-03-24 23:22:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
proxy.on('error', function(err, req) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error(err, req.url);
|
|
|
|
});
|
|
|
|
|
2020-04-10 21:12:20 +00:00
|
|
|
app.use(proxyPath, function(req, res) {
|
2020-03-24 23:22:16 +00:00
|
|
|
// include root path in proxied request
|
|
|
|
req.url = proxyPath + req.url;
|
2020-04-10 21:12:20 +00:00
|
|
|
proxy.web(req, res, { target: proxyAddress });
|
2020-03-24 23:22:16 +00:00
|
|
|
});
|
|
|
|
|
2020-04-10 21:12:20 +00:00
|
|
|
server.on('upgrade', function(req, socket, head) {
|
2020-03-24 23:22:16 +00:00
|
|
|
if (req.url.startsWith('/v1/client/allocation') && req.url.includes('exec?')) {
|
2020-04-10 21:12:20 +00:00
|
|
|
req.headers.origin = proxyAddress;
|
|
|
|
proxy.ws(req, socket, head, { target: proxyAddress });
|
2020-03-24 23:22:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|