1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var fs = require('fs');
|
32 | var rootPath = '/opt/qewd/mapped/';
|
33 |
|
34 | function updateConfig(json) {
|
35 | fs.writeFileSync(rootPath + 'configuration/config.json', JSON.stringify(json, null, 2));
|
36 | }
|
37 |
|
38 | function updateRoutes(json) {
|
39 | fs.writeFileSync(rootPath + 'configuration/routes.json', JSON.stringify(json, null, 2));
|
40 | }
|
41 |
|
42 | function setMsToImported(ms_name) {
|
43 | var _this = this;
|
44 | this.importMode.config_data.microservices.forEach(function(ms, index) {
|
45 |
|
46 | if (ms.name === ms_name) {
|
47 | _this.importMode.config_data.microservices[index].apis.imported = true;
|
48 | }
|
49 | });
|
50 | updateConfig(this.importMode.config_data);
|
51 | var routes = [];
|
52 | for (var uri in this.importMode.routesByUri) {
|
53 | routes.push(this.importMode.routesByUri[uri]);
|
54 | }
|
55 | updateRoutes(routes);
|
56 | }
|
57 |
|
58 | function orchShouldStop() {
|
59 | var shouldStop = true;
|
60 | this.importMode.config_data.microservices.forEach(function(ms) {
|
61 | if (ms.apis && ms.apis.import && !ms.apis.imported) {
|
62 | shouldStop = false;
|
63 | }
|
64 | });
|
65 |
|
66 | if (shouldStop) {
|
67 | updateConfig(this.importMode.config_data);
|
68 | var routes = [];
|
69 | for (var uri in this.importMode.routesByUri) {
|
70 | if (uri !== 'POST:/qewd/importRoutes/:destination') {
|
71 | routes.push(this.importMode.routesByUri[uri]);
|
72 | }
|
73 | }
|
74 | updateRoutes(routes);
|
75 | }
|
76 |
|
77 | return shouldStop;
|
78 | }
|
79 |
|
80 | function removePreviousMSRoutes(ms_name) {
|
81 |
|
82 | var route;
|
83 | for (var uri in this.importMode.routesByUri) {
|
84 | route = this.importMode.routesByUri[uri];
|
85 | if (route.on_microservice !== ms_name) {
|
86 | if (route.on_microservices) {
|
87 | var routeClone = Object.assign({}, route);
|
88 | var msArr = route.on_microservices.slice(0);
|
89 | var ix = msArr.indexOf(ms_name);
|
90 | if (ix !== -1) {
|
91 | msArr.splice(ix, 1);
|
92 | if (msArr.length === 1) {
|
93 | delete routeClone.on_microservices;
|
94 | routeClone.on_microservice = msArr[0];
|
95 | }
|
96 | if (msArr.length > 1) {
|
97 | routeClone.on_microservices = msArr;
|
98 | }
|
99 | if (msArr.length > 0) {
|
100 | this.importMode.routesByUri[uri] = routeClone;
|
101 | }
|
102 | }
|
103 | }
|
104 | }
|
105 | else {
|
106 | delete this.importMode.routesByUri[uri];
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | module.exports = function(ms_name, jwt) {
|
112 |
|
113 | if (!this.userDefined.config.qewd_up) {
|
114 |
|
115 | return;
|
116 | }
|
117 |
|
118 | if (!this.importMode) {
|
119 | this.importMode = {};
|
120 | }
|
121 |
|
122 | var routes;
|
123 | var _this = this;
|
124 |
|
125 | rootPath = '/opt/qewd/mapped/';
|
126 | if (process.env.qewd_service_name) {
|
127 | if (process.env.qewd_isNative) {
|
128 | rootPath = process.cwd() + '/';
|
129 | }
|
130 | else {
|
131 | rootPath = process.cwd() + '/' + process.env.qewd_service_name + '/';
|
132 | }
|
133 | }
|
134 |
|
135 | if (!this.importMode.config_data) {
|
136 | try {
|
137 | this.importMode.config_data = require(rootPath + 'configuration/config.json');
|
138 | }
|
139 | catch(err) {
|
140 | this.importMode.config_data = {};
|
141 | }
|
142 | try {
|
143 | routes = require(rootPath + 'configuration/routes.json');
|
144 | }
|
145 | catch(err) {
|
146 | routes = [];
|
147 | }
|
148 |
|
149 | this.importMode.routesByUri = {};
|
150 | routes.forEach(function(route) {
|
151 | if (route.uri !== '/qewd/importRoutes/:destination') {
|
152 | _this.importMode.routesByUri[route.method + ':' + route.uri] = route;
|
153 | }
|
154 | });
|
155 | }
|
156 |
|
157 | var ms;
|
158 | var import_ms = false;
|
159 | var replacePaths = {};
|
160 | var microservices = this.importMode.config_data.microservices;
|
161 |
|
162 | for (var i = 0; i < microservices.length; i++) {
|
163 | ms = microservices[i];
|
164 | if (ms.name === ms_name) {
|
165 | if (ms.apis && ms.apis.import && !ms.apis.imported) {
|
166 | import_ms = true;
|
167 |
|
168 | if (ms.apis.path) {
|
169 | if (ms.apis.path.replacePrefixWith) {
|
170 | replacePaths.prefix = ms.apis.path.replacePrefixWith;
|
171 | }
|
172 | if (ms.apis.path.prependWith) {
|
173 | replacePaths.prepend = ms.apis.path.prependWith;
|
174 | }
|
175 | }
|
176 | }
|
177 | }
|
178 | }
|
179 |
|
180 | if (import_ms) {
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | removePreviousMSRoutes.call(this, ms_name);
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | var message = {
|
195 | type: 'ewd-qoper8-express',
|
196 | method: 'POST',
|
197 | path: '/qewd/importRoutes/' + ms_name,
|
198 | jwt: jwt,
|
199 | body: {
|
200 | secret: this.userDefined.config.jwt.secret,
|
201 | pathPrefix: replacePaths.prefix,
|
202 | pathPrepend: replacePaths.prepend
|
203 | }
|
204 | };
|
205 |
|
206 | this.microServiceRouter(message, function(responseObj) {
|
207 |
|
208 | if (!responseObj.error) {
|
209 |
|
210 |
|
211 | var ms_name = responseObj.message.ms_name;
|
212 | if (responseObj.message && responseObj.message.routes) {
|
213 | var routesByUri = _this.importMode.routesByUri;
|
214 |
|
215 | responseObj.message.routes.forEach(function(route) {
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | if (replacePaths.prefix) {
|
221 | var pieces = route.uri.split('/');
|
222 | pieces[1] = replacePaths.prefix;
|
223 | route.uri = pieces.join('/');
|
224 | }
|
225 | if (replacePaths.prepend) {
|
226 | var prepend = replacePaths.prepend;
|
227 | if (prepend[0] !== '/') {
|
228 | prepend = '/' + prepend;
|
229 | }
|
230 | route.uri = prepend + route.uri;
|
231 | }
|
232 | var routeIndex = route.method + ':' + route.uri;
|
233 | if (routesByUri[routeIndex]) {
|
234 | if (!routesByUri[routeIndex].on_microservice && !routesByUri[routeIndex].on_microservices) {
|
235 | routesByUri[routeIndex].on_microservice = ms_name;
|
236 | }
|
237 | if (routesByUri[routeIndex].on_microservice && !routesByUri[routeIndex].on_microservices) {
|
238 | routesByUri[routeIndex].on_microservices = [
|
239 | ms_name
|
240 | ];
|
241 | delete routesByUri[routeIndex].on_microservice;
|
242 | }
|
243 | if (routesByUri[routeIndex].on_microservices) {
|
244 | if (routesByUri[routeIndex].on_microservices.indexOf(ms_name) === -1) {
|
245 | routesByUri[routeIndex].on_microservices.push(ms_name);
|
246 | }
|
247 | }
|
248 | }
|
249 | else {
|
250 | route.on_microservice = ms_name;
|
251 | }
|
252 | routesByUri[routeIndex] = route;
|
253 | });
|
254 | }
|
255 |
|
256 | setMsToImported.call(_this, ms_name);
|
257 | if (orchShouldStop.call(_this)) {
|
258 |
|
259 | _this.stop();
|
260 | }
|
261 | }
|
262 | else {
|
263 | console.log('** An error occurred while importing MicroService APIs: ' + responseObj.error);
|
264 | }
|
265 |
|
266 | });
|
267 | }
|
268 | };
|