Skip to content

Commit

Permalink
Fix expand srv json
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Sep 21, 2020
1 parent dfa1466 commit 20b0904
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kakadu-dev/nodejs-ijson-microservices",
"version": "1.5.0",
"version": "1.5.1",
"description": "Package for create microservice architecture based on NodeJS. ",
"keywords": [
"node",
Expand Down
27 changes: 23 additions & 4 deletions src/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Gateway
serviceParams: {},
};

/**
* @type {boolean} srv ijson expanded
*/
srvExpand = false;

/**
* @type {function(msg: string)}
*
Expand Down Expand Up @@ -142,6 +147,20 @@ class Gateway
return this.myInstance;
}

/**
* Get ijson host
*
* @return {Promise<string>}
*/
async getIjsonHost() {
if (!this.srvExpand) {
this.options.ijson = await ExpandSrv(this.options.ijson);
this.srvExpand = true;
}

return this.options.ijson;
}

/**
* Handle gateway request
*
Expand Down Expand Up @@ -274,7 +293,8 @@ class Gateway
},
}, reqParams);

return axios.post(`${this.options.ijson}/${name}`, new MjRequest(params), config);
return this.getIjsonHost()
.then(ijsonHost => axios.post(`${ijsonHost}/${name}`, new MjRequest(params), config));
}

/**
Expand All @@ -298,9 +318,8 @@ class Gateway
|| (({ name, port, version, env }) => console.info(`${name} gateway started on: ` +
`${port} port. Version: ${version} (${env})`));

ExpandSrv(this.options.ijson)
.then(ijsonHost => {
this.options.ijson = ijsonHost;
this.getIjsonHost()
.then(() => {
this.app.listen(this.options.port, () => clbck(this.options));
})
.catch(console.error);
Expand Down
29 changes: 24 additions & 5 deletions src/Microservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Microservice
requestTimeout: 1000 * 60 * 5, // 5 min
};

/**
* @type {boolean} srv ijson expanded
*/
srvExpand = false;

/**
* @type {Object}
*
Expand Down Expand Up @@ -144,6 +149,20 @@ class Microservice
return this;
}

/**
* Get ijson host
*
* @return {Promise<string>}
*/
async getIjsonHost() {
if (!this.srvExpand) {
this.options.ijson = await ExpandSrv(this.options.ijson);
this.srvExpand = true;
}

return this.options.ijson;
}

/**
* Send request to service
*
Expand All @@ -167,13 +186,14 @@ class Microservice
},
}));

let response = {};
const time = Date.now();
let response = {};
const time = Date.now();
const jsonHost = await this.getIjsonHost();

try {
this.logDriver(() => ` --> Request (${service} - ${request.getId()}): ${JSON.stringify(request)}`, 2, request.getId());

response = (await axios.post(`${this.options.ijson}/${service}`, request, {
response = (await axios.post(`${jsonHost}/${service}`, request, {
timeout: this.options.requestTimeout,
...requestConfig,
})).data;
Expand Down Expand Up @@ -256,7 +276,7 @@ class Microservice
try {
const resp = await axios.request({
url: isFirstTask ? `/${this.name}` : null,
baseURL: this.options.ijson,
baseURL: await this.getIjsonHost(),
method: 'post',
data: response,
httpAgent: this.httpAgent,
Expand Down Expand Up @@ -294,7 +314,6 @@ class Microservice
|| (({ version, env }) => console.info(`${this.name} microservice started. ` +
`Version: ${version} (${env})`));

this.options.ijson = await ExpandSrv(this.options.ijson);
clbck(this.options);

let request;
Expand Down

0 comments on commit 20b0904

Please sign in to comment.