From 0874909f16c80ad53e2e3ba8cfa7e12f03275c9e Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Mon, 23 Apr 2018 09:17:24 +0100 Subject: [PATCH] Enable generated app to accept REST server URLs for multiple business networks (#3891) Signed-off-by: Simon Stone --- .../generators/angular/index.js | 6 ++ .../angular/templates/proxy.conf.js | 21 +++- .../test/angular-archive.js | 96 +++++++++++++++++++ .../test/angular-network.js | 96 +++++++++++++++++++ 4 files changed, 217 insertions(+), 2 deletions(-) diff --git a/packages/generator-hyperledger-composer/generators/angular/index.js b/packages/generator-hyperledger-composer/generators/angular/index.js index d570299c0a..59d153fb4e 100755 --- a/packages/generator-hyperledger-composer/generators/angular/index.js +++ b/packages/generator-hyperledger-composer/generators/angular/index.js @@ -28,6 +28,8 @@ const { URL } = require('url'); let businessNetworkConnection; let businessNetworkDefinition; +let businessNetworkName; +let businessNetworkVersion; let businessNetworkIdentifier; let modelManager; let assetList = []; @@ -384,6 +386,8 @@ module.exports = yeoman.Base.extend({ let createdApp = new Promise((resolve, reject) => { + businessNetworkName = businessNetworkDefinition.getName(); + businessNetworkVersion = businessNetworkDefinition.getVersion(); businessNetworkIdentifier = businessNetworkDefinition.getIdentifier(); introspector = businessNetworkDefinition.getIntrospector(); @@ -899,6 +903,8 @@ module.exports = yeoman.Base.extend({ authorName: this.authorName, authorEmail: this.authorEmail, license: this.license, + businessNetworkName: businessNetworkName, + businessNetworkVersion: businessNetworkVersion, businessNetworkIdentifier: businessNetworkIdentifier, assetList: assetList, assetServiceNames: assetServiceNames, diff --git a/packages/generator-hyperledger-composer/generators/angular/templates/proxy.conf.js b/packages/generator-hyperledger-composer/generators/angular/templates/proxy.conf.js index f359db74b9..dfe4fb55f6 100644 --- a/packages/generator-hyperledger-composer/generators/angular/templates/proxy.conf.js +++ b/packages/generator-hyperledger-composer/generators/angular/templates/proxy.conf.js @@ -12,14 +12,31 @@ * limitations under the License. */ +function getTarget() { + if (process.env.REST_SERVER_URLS) { + const restServerURLs = JSON.parse(process.env.REST_SERVER_URLS); + const restServerURL = restServerURLs['<%= businessNetworkName %>']; + if (restServerURL) { + return restServerURL; + } + } + if (process.env.REST_SERVER_URL) { + const restServerURL = process.env.REST_SERVER_URL; + return restServerURL; + } + return '<%= apiURL %>'; +} + +const target = getTarget(); + module.exports = [{ context: ['/auth', '/api'], - target: process.env.REST_SERVER_URL || '<%= apiURL %>', + target, secure: true, changeOrigin: true }, { context: '/', - target: process.env.REST_SERVER_URL || '<%= apiURL %>', + target, secure: true, changeOrigin: true, ws: true, diff --git a/packages/generator-hyperledger-composer/test/angular-archive.js b/packages/generator-hyperledger-composer/test/angular-archive.js index dc2c760416..c21bfbf416 100755 --- a/packages/generator-hyperledger-composer/test/angular-archive.js +++ b/packages/generator-hyperledger-composer/test/angular-archive.js @@ -134,6 +134,16 @@ describe('hyperledger-composer:angular for CarAuction-Network running against a }); }); + beforeEach(() => { + delete process.env.REST_SERVER_URL; + delete process.env.REST_SERVER_URLS; + }); + + afterEach(() => { + delete process.env.REST_SERVER_URL; + delete process.env.REST_SERVER_URLS; + }); + it('creates typescript classes', function(){ assert.file(tmpDir+'/CarAuction-Network/src/app/org.acme.vehicle.auction.ts'); assert.fileContent(tmpDir+'/CarAuction-Network/src/app/org.acme.vehicle.auction.ts', @@ -329,6 +339,92 @@ import {Event} from './org.hyperledger.composer.system'; it('should create a suitable proxy.conf.js file', () => { const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'https://dogescoolrestserver.dogecorp.com:3000' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'https://dogescoolrestserver.dogecorp.com:3000', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment', () => { + process.env.REST_SERVER_URL = 'https://doges-other-rest-server.dogecorp.com:9999'; + const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment for this business network', () => { + process.env.REST_SERVER_URLS = JSON.stringify({ + 'carauction-network': 'https://doges-other-rest-server.dogecorp.com:9999' + }); + const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that ignores a REST server URL from the environment for another business network', () => { + process.env.REST_SERVER_URLS = JSON.stringify({ + 'someother-network': 'https://doges-other-rest-server.dogecorp.com:9999' + }); + const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; const proxyConfig = require(filePath); assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); delete proxyConfig[1].bypass; diff --git a/packages/generator-hyperledger-composer/test/angular-network.js b/packages/generator-hyperledger-composer/test/angular-network.js index 87f38f214c..aaebb5b81f 100755 --- a/packages/generator-hyperledger-composer/test/angular-network.js +++ b/packages/generator-hyperledger-composer/test/angular-network.js @@ -77,6 +77,16 @@ describe('hyperledger-composer:angular for digitalPropertyNetwork running agains }); + beforeEach(() => { + delete process.env.REST_SERVER_URL; + delete process.env.REST_SERVER_URLS; + }); + + afterEach(() => { + delete process.env.REST_SERVER_URL; + delete process.env.REST_SERVER_URLS; + }); + it('creates typescript classes', function(){ assert.file(tmpDir+'/digitalPropertyNetwork/src/app/net.biz.digitalPropertyNetwork.ts'); }); @@ -230,6 +240,92 @@ describe('hyperledger-composer:angular for digitalPropertyNetwork running agains it('should create a suitable proxy.conf.js file', () => { const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'http://localhost:3000' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'http://localhost:3000', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment', () => { + process.env.REST_SERVER_URL = 'https://doges-other-rest-server.dogecorp.com:9999'; + const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment for this business network', () => { + process.env.REST_SERVER_URLS = JSON.stringify({ + 'digitalproperty-network': 'https://doges-other-rest-server.dogecorp.com:9999' + }); + const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; + const proxyConfig = require(filePath); + assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); + delete proxyConfig[1].bypass; + assert.deepStrictEqual(proxyConfig, [ + { + changeOrigin: true, + context: [ + '/auth', + '/api' + ], + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999' + }, + { + changeOrigin: true, + context: '/', + secure: true, + target: 'https://doges-other-rest-server.dogecorp.com:9999', + ws: true + } + ], 'proxy configuration is wrong'); + }); + + it('should create a suitable proxy.conf.js file that ignores a REST server URL from the environment for another business network', () => { + process.env.REST_SERVER_URLS = JSON.stringify({ + 'someother-network': 'https://doges-other-rest-server.dogecorp.com:9999' + }); + const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js'; + delete require.cache[require.resolve(filePath)]; const proxyConfig = require(filePath); assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function'); delete proxyConfig[1].bypass;