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

Commit 0874909

Browse files
author
Simon Stone
authored
Enable generated app to accept REST server URLs for multiple business networks (#3891)
Signed-off-by: Simon Stone <[email protected]>
1 parent dd714e9 commit 0874909

File tree

4 files changed

+217
-2
lines changed

4 files changed

+217
-2
lines changed

packages/generator-hyperledger-composer/generators/angular/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const { URL } = require('url');
2828

2929
let businessNetworkConnection;
3030
let businessNetworkDefinition;
31+
let businessNetworkName;
32+
let businessNetworkVersion;
3133
let businessNetworkIdentifier;
3234
let modelManager;
3335
let assetList = [];
@@ -384,6 +386,8 @@ module.exports = yeoman.Base.extend({
384386

385387
let createdApp = new Promise((resolve, reject) => {
386388

389+
businessNetworkName = businessNetworkDefinition.getName();
390+
businessNetworkVersion = businessNetworkDefinition.getVersion();
387391
businessNetworkIdentifier = businessNetworkDefinition.getIdentifier();
388392
introspector = businessNetworkDefinition.getIntrospector();
389393

@@ -899,6 +903,8 @@ module.exports = yeoman.Base.extend({
899903
authorName: this.authorName,
900904
authorEmail: this.authorEmail,
901905
license: this.license,
906+
businessNetworkName: businessNetworkName,
907+
businessNetworkVersion: businessNetworkVersion,
902908
businessNetworkIdentifier: businessNetworkIdentifier,
903909
assetList: assetList,
904910
assetServiceNames: assetServiceNames,

packages/generator-hyperledger-composer/generators/angular/templates/proxy.conf.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,31 @@
1212
* limitations under the License.
1313
*/
1414

15+
function getTarget() {
16+
if (process.env.REST_SERVER_URLS) {
17+
const restServerURLs = JSON.parse(process.env.REST_SERVER_URLS);
18+
const restServerURL = restServerURLs['<%= businessNetworkName %>'];
19+
if (restServerURL) {
20+
return restServerURL;
21+
}
22+
}
23+
if (process.env.REST_SERVER_URL) {
24+
const restServerURL = process.env.REST_SERVER_URL;
25+
return restServerURL;
26+
}
27+
return '<%= apiURL %>';
28+
}
29+
30+
const target = getTarget();
31+
1532
module.exports = [{
1633
context: ['/auth', '/api'],
17-
target: process.env.REST_SERVER_URL || '<%= apiURL %>',
34+
target,
1835
secure: true,
1936
changeOrigin: true
2037
}, {
2138
context: '/',
22-
target: process.env.REST_SERVER_URL || '<%= apiURL %>',
39+
target,
2340
secure: true,
2441
changeOrigin: true,
2542
ws: true,

packages/generator-hyperledger-composer/test/angular-archive.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ describe('hyperledger-composer:angular for CarAuction-Network running against a
134134
});
135135
});
136136

137+
beforeEach(() => {
138+
delete process.env.REST_SERVER_URL;
139+
delete process.env.REST_SERVER_URLS;
140+
});
141+
142+
afterEach(() => {
143+
delete process.env.REST_SERVER_URL;
144+
delete process.env.REST_SERVER_URLS;
145+
});
146+
137147
it('creates typescript classes', function(){
138148
assert.file(tmpDir+'/CarAuction-Network/src/app/org.acme.vehicle.auction.ts');
139149
assert.fileContent(tmpDir+'/CarAuction-Network/src/app/org.acme.vehicle.auction.ts',
@@ -329,6 +339,92 @@ import {Event} from './org.hyperledger.composer.system';
329339

330340
it('should create a suitable proxy.conf.js file', () => {
331341
const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js';
342+
delete require.cache[require.resolve(filePath)];
343+
const proxyConfig = require(filePath);
344+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
345+
delete proxyConfig[1].bypass;
346+
assert.deepStrictEqual(proxyConfig, [
347+
{
348+
changeOrigin: true,
349+
context: [
350+
'/auth',
351+
'/api'
352+
],
353+
secure: true,
354+
target: 'https://dogescoolrestserver.dogecorp.com:3000'
355+
},
356+
{
357+
changeOrigin: true,
358+
context: '/',
359+
secure: true,
360+
target: 'https://dogescoolrestserver.dogecorp.com:3000',
361+
ws: true
362+
}
363+
], 'proxy configuration is wrong');
364+
});
365+
366+
it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment', () => {
367+
process.env.REST_SERVER_URL = 'https://doges-other-rest-server.dogecorp.com:9999';
368+
const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js';
369+
delete require.cache[require.resolve(filePath)];
370+
const proxyConfig = require(filePath);
371+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
372+
delete proxyConfig[1].bypass;
373+
assert.deepStrictEqual(proxyConfig, [
374+
{
375+
changeOrigin: true,
376+
context: [
377+
'/auth',
378+
'/api'
379+
],
380+
secure: true,
381+
target: 'https://doges-other-rest-server.dogecorp.com:9999'
382+
},
383+
{
384+
changeOrigin: true,
385+
context: '/',
386+
secure: true,
387+
target: 'https://doges-other-rest-server.dogecorp.com:9999',
388+
ws: true
389+
}
390+
], 'proxy configuration is wrong');
391+
});
392+
393+
it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment for this business network', () => {
394+
process.env.REST_SERVER_URLS = JSON.stringify({
395+
'carauction-network': 'https://doges-other-rest-server.dogecorp.com:9999'
396+
});
397+
const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js';
398+
delete require.cache[require.resolve(filePath)];
399+
const proxyConfig = require(filePath);
400+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
401+
delete proxyConfig[1].bypass;
402+
assert.deepStrictEqual(proxyConfig, [
403+
{
404+
changeOrigin: true,
405+
context: [
406+
'/auth',
407+
'/api'
408+
],
409+
secure: true,
410+
target: 'https://doges-other-rest-server.dogecorp.com:9999'
411+
},
412+
{
413+
changeOrigin: true,
414+
context: '/',
415+
secure: true,
416+
target: 'https://doges-other-rest-server.dogecorp.com:9999',
417+
ws: true
418+
}
419+
], 'proxy configuration is wrong');
420+
});
421+
422+
it('should create a suitable proxy.conf.js file that ignores a REST server URL from the environment for another business network', () => {
423+
process.env.REST_SERVER_URLS = JSON.stringify({
424+
'someother-network': 'https://doges-other-rest-server.dogecorp.com:9999'
425+
});
426+
const filePath = tmpDir + '/CarAuction-Network/proxy.conf.js';
427+
delete require.cache[require.resolve(filePath)];
332428
const proxyConfig = require(filePath);
333429
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
334430
delete proxyConfig[1].bypass;

packages/generator-hyperledger-composer/test/angular-network.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ describe('hyperledger-composer:angular for digitalPropertyNetwork running agains
7777

7878
});
7979

80+
beforeEach(() => {
81+
delete process.env.REST_SERVER_URL;
82+
delete process.env.REST_SERVER_URLS;
83+
});
84+
85+
afterEach(() => {
86+
delete process.env.REST_SERVER_URL;
87+
delete process.env.REST_SERVER_URLS;
88+
});
89+
8090
it('creates typescript classes', function(){
8191
assert.file(tmpDir+'/digitalPropertyNetwork/src/app/net.biz.digitalPropertyNetwork.ts');
8292
});
@@ -230,6 +240,92 @@ describe('hyperledger-composer:angular for digitalPropertyNetwork running agains
230240

231241
it('should create a suitable proxy.conf.js file', () => {
232242
const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js';
243+
delete require.cache[require.resolve(filePath)];
244+
const proxyConfig = require(filePath);
245+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
246+
delete proxyConfig[1].bypass;
247+
assert.deepStrictEqual(proxyConfig, [
248+
{
249+
changeOrigin: true,
250+
context: [
251+
'/auth',
252+
'/api'
253+
],
254+
secure: true,
255+
target: 'http://localhost:3000'
256+
},
257+
{
258+
changeOrigin: true,
259+
context: '/',
260+
secure: true,
261+
target: 'http://localhost:3000',
262+
ws: true
263+
}
264+
], 'proxy configuration is wrong');
265+
});
266+
267+
it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment', () => {
268+
process.env.REST_SERVER_URL = 'https://doges-other-rest-server.dogecorp.com:9999';
269+
const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js';
270+
delete require.cache[require.resolve(filePath)];
271+
const proxyConfig = require(filePath);
272+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
273+
delete proxyConfig[1].bypass;
274+
assert.deepStrictEqual(proxyConfig, [
275+
{
276+
changeOrigin: true,
277+
context: [
278+
'/auth',
279+
'/api'
280+
],
281+
secure: true,
282+
target: 'https://doges-other-rest-server.dogecorp.com:9999'
283+
},
284+
{
285+
changeOrigin: true,
286+
context: '/',
287+
secure: true,
288+
target: 'https://doges-other-rest-server.dogecorp.com:9999',
289+
ws: true
290+
}
291+
], 'proxy configuration is wrong');
292+
});
293+
294+
it('should create a suitable proxy.conf.js file that uses a REST server URL from the environment for this business network', () => {
295+
process.env.REST_SERVER_URLS = JSON.stringify({
296+
'digitalproperty-network': 'https://doges-other-rest-server.dogecorp.com:9999'
297+
});
298+
const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js';
299+
delete require.cache[require.resolve(filePath)];
300+
const proxyConfig = require(filePath);
301+
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
302+
delete proxyConfig[1].bypass;
303+
assert.deepStrictEqual(proxyConfig, [
304+
{
305+
changeOrigin: true,
306+
context: [
307+
'/auth',
308+
'/api'
309+
],
310+
secure: true,
311+
target: 'https://doges-other-rest-server.dogecorp.com:9999'
312+
},
313+
{
314+
changeOrigin: true,
315+
context: '/',
316+
secure: true,
317+
target: 'https://doges-other-rest-server.dogecorp.com:9999',
318+
ws: true
319+
}
320+
], 'proxy configuration is wrong');
321+
});
322+
323+
it('should create a suitable proxy.conf.js file that ignores a REST server URL from the environment for another business network', () => {
324+
process.env.REST_SERVER_URLS = JSON.stringify({
325+
'someother-network': 'https://doges-other-rest-server.dogecorp.com:9999'
326+
});
327+
const filePath = tmpDir + '/digitalPropertyNetwork/proxy.conf.js';
328+
delete require.cache[require.resolve(filePath)];
233329
const proxyConfig = require(filePath);
234330
assert(typeof proxyConfig[1].bypass === 'function', 'no bypass function');
235331
delete proxyConfig[1].bypass;

0 commit comments

Comments
 (0)