1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const _ = require('underscore');
|
8 | const vcapenv = require('abacus-vcapenv');
|
9 |
|
10 | const object = _.object;
|
11 | const map = _.map;
|
12 | const keys = _.keys;
|
13 | const some = _.some;
|
14 |
|
15 |
|
16 | const debug = require('abacus-debug')('abacus-urienv');
|
17 |
|
18 |
|
19 | const defprotocol = () => process.browser ? window.location.protocol : 'http:';
|
20 |
|
21 |
|
22 | const defhost = () => process.browser ? window.location.hostname : 'localhost';
|
23 |
|
24 |
|
25 | const defport = () => process.browser ? window.location.port : process.env.PORT ? process.env.PORT : 9080;
|
26 |
|
27 |
|
28 | const env = (alias) => {
|
29 | const value = process.env[alias.replace('-', '_').toUpperCase()];
|
30 | return value && value.includes('|') ? value.split('|') : value;
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 | const serviceInstanceURIs = (alias) => {
|
36 | const serviceURIs = vcapenv.serviceInstancesCredentials(alias, 'uri');
|
37 | if (serviceURIs.every((serviceURI) => /:/.test(serviceURI))) {
|
38 | debug('Resolved %s to service instance URIs %o', alias, serviceURIs);
|
39 | return serviceURIs;
|
40 | }
|
41 |
|
42 | return [];
|
43 | };
|
44 |
|
45 | const validDomain = (primaryDomain, uris) => {
|
46 | return some(uris, (uri) => {
|
47 | return uri.includes(primaryDomain);
|
48 | });
|
49 | };
|
50 |
|
51 | const domain = (uris) => {
|
52 | const primaryDomain = process.env.PRIMARY_DOMAIN;
|
53 |
|
54 | const domain =
|
55 | primaryDomain && validDomain(primaryDomain, uris)
|
56 | ? primaryDomain
|
57 | : uris[0].split('.').slice(1).join('.');
|
58 |
|
59 | debug('Using domain: %s', domain);
|
60 | return domain;
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 | const hosted = (alias, uris) => {
|
66 |
|
67 | if (vcapenv.services()) {
|
68 | debug('Searching in service instances %j', vcapenv.services());
|
69 | const serviceURIs = serviceInstanceURIs(alias);
|
70 | if (serviceURIs && serviceURIs.length > 0) return serviceURIs;
|
71 | }
|
72 |
|
73 |
|
74 | const resolved = env(alias) || alias;
|
75 | if (Array.isArray(resolved) && resolved.every((r) => /:/.test(r))) {
|
76 | debug('Resolved env alias %s to URIs %o', alias, resolved);
|
77 | return resolved;
|
78 | }
|
79 | if (/:/.test(resolved)) {
|
80 | debug('Resolved env alias %s to URI %s', alias, resolved);
|
81 | return resolved;
|
82 | }
|
83 |
|
84 | const target = 'https:' + '//' + resolved + '.' + domain(uris);
|
85 | debug('Resolved app route %s to %s', alias, target);
|
86 | return target;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | const local = (alias, def) => {
|
93 |
|
94 | const resolved = env(alias) || def;
|
95 | if (resolved) {
|
96 | if (Array.isArray(resolved)) {
|
97 | if (resolved.every((r) => /^[0-9]+$/.test(r))) {
|
98 | const target = resolved.map((r) => defprotocol() + '//' + defhost() + ':' + r);
|
99 | debug('Resolved alias %s to ports %o', alias, target);
|
100 | return target;
|
101 | }
|
102 | debug('Resolved alias %s to URIs %o', alias, resolved);
|
103 | return resolved;
|
104 | }
|
105 | if (/^[0-9]+$/.test(resolved)) {
|
106 | const target = defprotocol() + '//' + defhost() + ':' + resolved;
|
107 | debug('Resolved alias %s to port %s', alias, target);
|
108 | return target;
|
109 | }
|
110 | debug('Resolved alias %s to URI %s', alias, resolved);
|
111 | return resolved;
|
112 | }
|
113 |
|
114 |
|
115 | const target = defprotocol() + '//' + defhost() + (defport() !== '' ? ':' + defport() : '');
|
116 | debug('Resolved alias %s to default %s', alias, target);
|
117 | return target;
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | const url = (alias, def) => {
|
124 |
|
125 | const uris = (vcapenv.app() || {}).application_uris;
|
126 | if (uris !== undefined && uris.length) return hosted(alias, uris);
|
127 |
|
128 |
|
129 | return local(alias, def);
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | const resolve = (apps) => object(map(keys(apps), (k) => [k, url(k, apps[k])]));
|
139 |
|
140 |
|
141 | module.exports = resolve;
|
142 | module.exports.resolve = resolve;
|
143 | module.exports.url = url;
|