Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Enable generated app to accept REST server URLs for multiple business…
Browse files Browse the repository at this point in the history
… networks (#3891)

Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored Apr 23, 2018
1 parent dd714e9 commit 0874909
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const { URL } = require('url');

let businessNetworkConnection;
let businessNetworkDefinition;
let businessNetworkName;
let businessNetworkVersion;
let businessNetworkIdentifier;
let modelManager;
let assetList = [];
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
96 changes: 96 additions & 0 deletions packages/generator-hyperledger-composer/test/angular-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
96 changes: 96 additions & 0 deletions packages/generator-hyperledger-composer/test/angular-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0874909

Please sign in to comment.