UNPKG

4.56 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 8 March 2019
28
29*/
30
31 // will be moved during startup to /apis/importRoutes/index.js
32 // if config.json shows imported as false
33 // and if running in import mode
34
35var routes = require('/opt/qewd/mapped/configuration/routes.json');
36var config = require('/opt/qewd/mapped/configuration/config.json');
37var fs = require('fs');
38
39function updateConfig(json) {
40 fs.writeFileSync('/opt/qewd/mapped/configuration/config.json', JSON.stringify(json, null, 2));
41}
42
43function isEmpty(obj) {
44 for (var name in obj) {
45 return false;
46 }
47 return true;
48}
49
50function updateRoutes(json) {
51 fs.writeFileSync('/opt/qewd/mapped/configuration/routes.json', JSON.stringify(json, null, 2));
52}
53
54module.exports = function(args, finished) {
55
56 // remove temporary importRoutes path if still present (eg due to previous error)
57 // and remove any previously imported routes (with modified uri paths)
58
59 var newRoutes = [];
60 routes.forEach(function(route) {
61 if (route.uri !== '/qewd/importRoutes/:destination' && !route.imported) {
62 newRoutes.push(route);
63 }
64 });
65
66 routes = newRoutes;
67
68 //console.log('updated routes: ' + JSON.stringify(routes, null, 2));
69
70 // update the JWT secret with the one from the Orchestrator
71
72 config.jwt = {
73 secret: args.req.body.secret
74 };
75
76 // flag this microservice as imported to prevent this happening next restart
77
78 config.imported = true;
79
80 if (isEmpty(config.qewd)) {
81 delete config.qewd;
82 };
83
84 updateConfig(config);
85
86 // replace routes with importRoutes route and
87 // on_microservice properties removed
88
89 var routes_data = routes.slice(); // clone the array
90 var newRoutes = [];
91 var pathPrefix = args.req.body.pathPrefix;
92 var pathPrepend = args.req.body.pathPrepend;
93 routes_data.forEach(function(route, index) {
94 var newRoute;
95 if (route.on_microservice === config.ms_name) {
96 delete routes_data[index].on_microservice;
97 }
98 // create additional versions using the path prefix sent from orchestrator (if present)
99 if (pathPrefix) {
100 newRoute = Object.assign({}, route);
101 var pieces = newRoute.uri.split('/');
102 pieces[1] = pathPrefix;
103 newRoute.uri = pieces.join('/');
104 newRoute.imported = true;
105 newRoutes.push(newRoute);
106 }
107 if (pathPrepend) {
108 newRoute = Object.assign({}, route);
109 if (pathPrepend[0] !== '/') {
110 pathPrepend = '/' + pathPrepend;
111 }
112 newRoute.uri = pathPrepend + newRoute.uri;
113 newRoute.imported = true;
114 newRoutes.push(newRoute);
115 }
116 });
117
118 updateRoutes(routes_data.concat(newRoutes));
119
120 finished({
121 routes: routes,
122 ms_name: config.ms_name
123 });
124};