UNPKG

9.32 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-up: Rapid QEWD API Development |
5 | |
6 | Copyright (c) 2018-20 M/Gateway Developments Ltd, |
7 | Redhill, Surrey UK. |
8 | All rights reserved. |
9 | |
10 | http://www.mgateway.com |
11 | Email: rtweed@mgateway.com |
12 | |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 12 May 2020
28
29*/
30
31var fs = require('fs');
32var rootPath = '/opt/qewd/mapped/';
33
34function updateConfig(json) {
35 fs.writeFileSync(rootPath + 'configuration/config.json', JSON.stringify(json, null, 2));
36}
37
38function updateRoutes(json) {
39 fs.writeFileSync(rootPath + 'configuration/routes.json', JSON.stringify(json, null, 2));
40}
41
42function setMsToImported(ms_name) {
43 var _this = this;
44 this.importMode.config_data.microservices.forEach(function(ms, index) {
45 // reset flags for all microservices that have been successfully mapped
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
58function 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
80function 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
111module.exports = function(ms_name, jwt) {
112
113 if (!this.userDefined.config.qewd_up) {
114 //console.log('*** this.userDefined.config.qewd_up is not set, so no further action');
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 // this microservice needs to be imported
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 // This microservice is flagged for import but has not yet been imported
183
184 // This might be a re-import for this microservice, so remove any
185 // previously added routes for this microservice
186
187 removePreviousMSRoutes.call(this, ms_name);
188
189 //console.log(JSON.stringify(this.importMode.routesByUri, null, 2));
190
191 // note: temporary route to allow import of microservice routes
192 // has already been added in run.js
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 //console.log('importRoutes response: ' + JSON.stringify(responseObj, null, 2));
208 if (!responseObj.error) {
209 // map microservice routes back into routes.json
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 // reset path if needed ***
217 // check if this is a duplicate route for another microservice, in which
218 // case on_microservice needs replacing with on_microservices array
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 // all microservices flagged for import have been imported
259 _this.stop();
260 }
261 }
262 else {
263 console.log('** An error occurred while importing MicroService APIs: ' + responseObj.error);
264 }
265
266 });
267 }
268};