From 4c0b4c2410fd22cc8fc97f93425b109d80952eeb Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 18 Sep 2018 12:12:17 +0100 Subject: [PATCH] Delay the start of the proxy --- lib/modules/blockchain_process/blockchain.js | 19 ++++++++++--------- lib/modules/blockchain_process/proxy.js | 6 +++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/modules/blockchain_process/blockchain.js b/lib/modules/blockchain_process/blockchain.js index a7f996f5c..90e4a1619 100644 --- a/lib/modules/blockchain_process/blockchain.js +++ b/lib/modules/blockchain_process/blockchain.js @@ -58,8 +58,6 @@ var Blockchain = function(options) { verbosity: this.blockchainConfig.verbosity }; - this.setupProxy(); - if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') { this.config.account = {}; this.config.account.password = fs.embarkPath("templates/boilerplate/config/development/password"); @@ -80,26 +78,28 @@ var Blockchain = function(options) { console.error(__(spaceMessage, 'genesisBlock')); process.exit(); } - + this.initProxy(); this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev}); }; -Blockchain.prototype.setupProxy = function() { +Blockchain.prototype.initProxy = function() { this.config.proxy = true; if (this.blockchainConfig.proxy === false) { this.config.proxy = false; return; } + this.config.rpcPort += constants.blockchain.servicePortOnProxy; + this.config.wsPort += constants.blockchain.servicePortOnProxy; +}; + +Blockchain.prototype.setupProxy = function() { const proxy = require('./proxy'); const Ipc = require('../../core/ipc'); let ipcObject = new Ipc({ipcRole: 'client'}); - this.rpcProxy = proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false); this.wsProxy = proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true); - this.config.rpcPort += constants.blockchain.servicePortOnProxy; - this.config.wsPort += constants.blockchain.servicePortOnProxy; }; Blockchain.prototype.shutdownProxy = function() { @@ -126,7 +126,6 @@ Blockchain.prototype.run = function() { console.log(__("Embark Blockchain Using: %s", this.client.name.underline).magenta); console.log("===============================================================================".magenta); console.log("===============================================================================".magenta); - this.checkPathLength(); let address = ''; async.waterfall([ @@ -150,6 +149,9 @@ Blockchain.prototype.run = function() { }, function getMainCommand(next) { self.client.mainCommand(address, function(cmd, args) { + if (self.config.proxy) { + self.setupProxy(); + } next(null, cmd, args); }, true); } @@ -163,7 +165,6 @@ Blockchain.prototype.run = function() { let full_cmd = cmd + " " + args.join(' '); console.log(__("running: %s", full_cmd.underline).green); self.child = child_process.spawn(cmd, args, {cwd: process.cwd()}); - self.child.on('error', (err) => { err = err.toString(); console.error('Blockchain error: ', err); diff --git a/lib/modules/blockchain_process/proxy.js b/lib/modules/blockchain_process/proxy.js index 38c7c5315..8779bae75 100644 --- a/lib/modules/blockchain_process/proxy.js +++ b/lib/modules/blockchain_process/proxy.js @@ -67,7 +67,7 @@ exports.serve = function (ipc, host, port, ws) { let proxy = httpProxy.createProxyServer({ target: { host: canonicalHost(host), - port: port + constants.blockchain.servicePortOnProxy + port: port }, ws: ws }); @@ -126,7 +126,7 @@ exports.serve = function (ipc, host, port, ws) { }); } - - server.listen(port, defaultHost); + const listenPort = port - constants.blockchain.servicePortOnProxy; + server.listen(listenPort, defaultHost); return server; };