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 | 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 |
|
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 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | removePreviousMSRoutes.call(this, ms_name);
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
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 |
|
203 | if (!responseObj.error) {
|
204 |
|
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 |
|
212 |
|
213 |
|
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 |
|
254 | _this.stop();
|
255 | }
|
256 | }
|
257 | else {
|
258 | console.log('** An error occurred while importing MicroService APIs: ' + responseObj.error);
|
259 | }
|
260 |
|
261 | });
|
262 | }
|
263 | };
|