1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd: Quick and Easy Web 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 | 8 March 2019
|
28 |
|
29 | Run during master process startup (see master.js)
|
30 | Sets up MicroService connections and control data structues
|
31 | as defined in the QEWD startup file
|
32 |
|
33 | */
|
34 |
|
35 | var qewdSocketClient = require('./socketClient');
|
36 | var router = require('qewd-router');
|
37 | var import_qewd_up_modules = require('../up/importModules');
|
38 |
|
39 | var count = 0;
|
40 | var noOfClients = 0;
|
41 |
|
42 | function addClient(url, application, startParams) {
|
43 | console.log('Adding MicroService Client connection: url = ' + url + '; application = ' + application);
|
44 | var q = this;
|
45 | if (!this.u_services.clients[url]) {
|
46 | this.u_services.clients[url] = new qewdSocketClient();
|
47 | startParams[url] = {
|
48 | url: url,
|
49 | application: application,
|
50 | log: true,
|
51 | jwt: this.jwt
|
52 | };
|
53 | this.u_services.clients[url].on('ewd-registered', function() {
|
54 | console.log(url + ' micro-service ready');
|
55 |
|
56 | // save registration token (needed for REST micro-service paths
|
57 |
|
58 | q.u_services.clients[url].token = this.token;
|
59 |
|
60 | // if we're using QEWD-Up, send the getRoutes message if it's to be imported
|
61 | // application tells us the microservice name that has just been registered
|
62 |
|
63 | if (q.userDefined.config.qewd_up) {
|
64 | import_qewd_up_modules.call(q, application, this.token);
|
65 | }
|
66 |
|
67 | });
|
68 | noOfClients++;
|
69 | }
|
70 | return this.u_services.clients[url];
|
71 | }
|
72 |
|
73 | module.exports = function(services_config) {
|
74 |
|
75 | console.log('Setting up micro-service connections');
|
76 | // set up micro-service connections
|
77 | var application;
|
78 | var path;
|
79 | var type;
|
80 | var serviceType;
|
81 | var url;
|
82 | var client;
|
83 | var startParams = {};
|
84 |
|
85 | this.u_services = {
|
86 | clients: {},
|
87 | byApplication: {},
|
88 | byPath: {},
|
89 | restRoutes: [],
|
90 | byDestination: {}
|
91 | };
|
92 |
|
93 | var q = this;
|
94 |
|
95 | if (!services_config.destinations && !services_config.routes) {
|
96 | var routes = services_config.slice(0); // clone the routes array
|
97 | services_config = {
|
98 | destinations: {},
|
99 | routes: routes
|
100 | };
|
101 | }
|
102 |
|
103 | var destObj;
|
104 | var client;
|
105 | for (var destination in services_config.destinations) {
|
106 | destObj = services_config.destinations[destination];
|
107 | if (destObj.application && destObj.host) {
|
108 | client = addClient.call(q, destObj.host, destObj.application, startParams);
|
109 | destObj.client = client;
|
110 | }
|
111 | q.u_services.byDestination[destination] = destObj;
|
112 | }
|
113 |
|
114 | services_config.routes.forEach(function(service) {
|
115 |
|
116 | var path = service.path;
|
117 |
|
118 | if (path) {
|
119 | // Rest Microservice routes - we'll handle these using qewd-router
|
120 |
|
121 | var route = {
|
122 | pathTemplate: path,
|
123 | destination: service.destination,
|
124 | method: service.method,
|
125 | onResponse: service.onResponse,
|
126 | bypassJWTCheck: service.bypassJWTCheck,
|
127 | route: new router.routeParser(service.path)
|
128 | };
|
129 |
|
130 | if (service.onRequest) {
|
131 | route.onRequest = service.onRequest;
|
132 | //delete route.destination; // onRequest function will determine the destination
|
133 | //delete route.onResponse; // onRequest's responsibility is to route to a destination which may have an onResponse
|
134 | }
|
135 |
|
136 | q.u_services.restRoutes.push(route);
|
137 |
|
138 | // make qewd-router accessible to ewd-qoper8-express which will handle the run-time routing
|
139 | if (!q.router) q.router = router;
|
140 | }
|
141 | else {
|
142 |
|
143 | // QEWD WebSocket Application MicroService routes
|
144 |
|
145 | application = service.application;
|
146 | if (application && !q.u_services.byApplication[application]) {
|
147 | q.u_services.byApplication[application] = {};
|
148 | }
|
149 | for (type in service.types) {
|
150 | //console.log('**** type = ' + type);
|
151 | serviceType = service.types[type];
|
152 |
|
153 | // route defined with host url and application, or with destination
|
154 |
|
155 | if (serviceType.url && serviceType.application) {
|
156 | client = addClient.call(q, serviceType.url, serviceType.application, startParams);
|
157 | q.u_services.byApplication[application][type] = {
|
158 | application: serviceType.application,
|
159 | type: type,
|
160 | client: client
|
161 | };
|
162 | }
|
163 | else if (serviceType.destination && services_config.destinations[serviceType.destination]) {
|
164 | client = q.u_services.clients[services_config.destinations[serviceType.destination].host];
|
165 | q.u_services.byApplication[application][type] = {
|
166 | application: services_config.destinations[serviceType.destination].application,
|
167 | type: type,
|
168 | client: client
|
169 | };
|
170 | }
|
171 | }
|
172 | }
|
173 | });
|
174 |
|
175 | // now start up the socket connections to the remote micro-socket servers
|
176 |
|
177 | // only when these connections are in place will this QEWD instance start
|
178 |
|
179 | for (url in this.u_services.clients) {
|
180 | console.log('starting microService connection to ' + url);
|
181 | this.u_services.clients[url].start(startParams[url]);
|
182 | }
|
183 |
|
184 | //console.log('u_services by application: ' + JSON.stringify(q.u_services.byApplication));
|
185 |
|
186 | };
|