UNPKG

8.96 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-up: Rapid QEWD API Development |
5 | |
6 | Copyright (c) 2018-19 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 4 July 2019
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 rootPath = process.cwd() + '/' + process.env.qewd_service_name + '/';
128 }
129
130 if (!this.importMode.config_data) {
131 try {
132 this.importMode.config_data = require(rootPath + 'configuration/config.json');
133 }
134 catch(err) {
135 this.importMode.config_data = {};
136 }
137 try {
138 routes = require(rootPath + 'configuration/routes.json');
139 }
140 catch(err) {
141 routes = [];
142 }
143
144 this.importMode.routesByUri = {};
145 routes.forEach(function(route) {
146 if (route.uri !== '/qewd/importRoutes/:destination') {
147 _this.importMode.routesByUri[route.method + ':' + route.uri] = route;
148 }
149 });
150 }
151
152 var ms;
153 var import_ms = false;
154 var replacePaths = {};
155 var microservices = this.importMode.config_data.microservices;
156
157 for (var i = 0; i < microservices.length; i++) {
158 ms = microservices[i];
159 if (ms.name === ms_name) {
160 if (ms.apis && ms.apis.import && !ms.apis.imported) {
161 import_ms = true;
162 // this microservice needs to be imported
163 if (ms.apis.path) {
164 if (ms.apis.path.replacePrefixWith) {
165 replacePaths.prefix = ms.apis.path.replacePrefixWith;
166 }
167 if (ms.apis.path.prependWith) {
168 replacePaths.prepend = ms.apis.path.prependWith;
169 }
170 }
171 }
172 }
173 }
174
175 if (import_ms) {
176
177 // This microservice is flagged for import but has not yet been imported
178
179 // This might be a re-import for this microservice, so remove any
180 // previously added routes for this microservice
181
182 removePreviousMSRoutes.call(this, ms_name);
183
184 //console.log(JSON.stringify(this.importMode.routesByUri, null, 2));
185
186 // note: temporary route to allow import of microservice routes
187 // has already been added in run.js
188
189 var message = {
190 type: 'ewd-qoper8-express',
191 method: 'POST',
192 path: '/qewd/importRoutes/' + ms_name,
193 jwt: jwt,
194 body: {
195 secret: this.userDefined.config.jwt.secret,
196 pathPrefix: replacePaths.prefix,
197 pathPrepend: replacePaths.prepend
198 }
199 };
200
201 this.microServiceRouter(message, function(responseObj) {
202 //console.log('importRoutes response: ' + JSON.stringify(responseObj, null, 2));
203 if (!responseObj.error) {
204 // map microservice routes back into routes.json
205
206 var ms_name = responseObj.message.ms_name;
207 if (responseObj.message && responseObj.message.routes) {
208 var routesByUri = _this.importMode.routesByUri;
209
210 responseObj.message.routes.forEach(function(route) {
211 // reset path if needed ***
212 // check if this is a duplicate route for another microservice, in which
213 // case on_microservice needs replacing with on_microservices array
214
215 if (replacePaths.prefix) {
216 var pieces = route.uri.split('/');
217 pieces[1] = replacePaths.prefix;
218 route.uri = pieces.join('/');
219 }
220 if (replacePaths.prepend) {
221 var prepend = replacePaths.prepend;
222 if (prepend[0] !== '/') {
223 prepend = '/' + prepend;
224 }
225 route.uri = prepend + route.uri;
226 }
227 var routeIndex = route.method + ':' + route.uri;
228 if (routesByUri[routeIndex]) {
229 if (!routesByUri[routeIndex].on_microservice && !routesByUri[routeIndex].on_microservices) {
230 routesByUri[routeIndex].on_microservice = ms_name;
231 }
232 if (routesByUri[routeIndex].on_microservice && !routesByUri[routeIndex].on_microservices) {
233 routesByUri[routeIndex].on_microservices = [
234 ms_name
235 ];
236 delete routesByUri[routeIndex].on_microservice;
237 }
238 if (routesByUri[routeIndex].on_microservices) {
239 if (routesByUri[routeIndex].on_microservices.indexOf(ms_name) === -1) {
240 routesByUri[routeIndex].on_microservices.push(ms_name);
241 }
242 }
243 }
244 else {
245 route.on_microservice = ms_name;
246 }
247 routesByUri[routeIndex] = route;
248 });
249 }
250
251 setMsToImported.call(_this, ms_name);
252 if (orchShouldStop.call(_this)) {
253 // all microservices flagged for import have been imported
254 _this.stop();
255 }
256 }
257 else {
258 console.log('** An error occurred while importing MicroService APIs: ' + responseObj.error);
259 }
260
261 });
262 }
263};