@haku-sci/utils 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Description of the utils inside this package
|
|
2
2
|
|
|
3
3
|
This package is used by Haku-Sci, to handle microservices.
|
|
4
|
-
|
|
4
|
+
Loading the class MicroserviceLib, it allows to fetch:
|
|
5
5
|
|
|
6
6
|
* The name of the current microservice
|
|
7
|
-
* The list of services for configuration (Based on the environment variable "SERVICES"), and their port
|
|
8
|
-
* The port of the current microservice (Based on the environment variable "SERVICES" and this service's name)
|
|
9
7
|
* default bootstrap for microservices
|
|
8
|
+
|
|
9
|
+
if consul is used, and env variabe CONSUL_URI is defined, then the lib allows as well to send messages to other services via TCP, and fetch the response if any.
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"@nestjs/testing": "^10.4.1",
|
|
10
10
|
"@types/jest": "^29.5.12"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.2.
|
|
13
|
-
"description": "utils from haku-sci. Library only
|
|
12
|
+
"version": "1.2.3",
|
|
13
|
+
"description": "utils from haku-sci. Library only",
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { DynamicModule, INestMicroservice, Module } from '@nestjs/common';
|
|
1
|
+
import { DynamicModule, INestMicroservice, Module, NotFoundException } from '@nestjs/common';
|
|
2
2
|
import { readFile } from 'fs/promises';
|
|
3
3
|
import * as path from 'path';
|
|
4
4
|
import { NestFactory } from '@nestjs/core';
|
|
5
|
-
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
|
|
5
|
+
import { ClientProxy, ClientProxyFactory, MicroserviceOptions, Transport } from '@nestjs/microservices';
|
|
6
6
|
import * as net from 'net';
|
|
7
7
|
import axios from 'axios';
|
|
8
|
+
import { lastValueFrom } from 'rxjs';
|
|
8
9
|
|
|
9
10
|
export class MicroserviceLib {
|
|
10
11
|
static async projectName(): Promise<string> {
|
|
@@ -18,6 +19,38 @@ export class MicroserviceLib {
|
|
|
18
19
|
return name;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
private static async getServiceURI(serviceName: string) :Promise<{host: string, port: number}> {
|
|
23
|
+
const response = await axios.get(`${process.env["CONSUL_URL"]}/v1/catalog/service/${serviceName}`);
|
|
24
|
+
const serviceInfo = response.data;
|
|
25
|
+
|
|
26
|
+
if (serviceInfo.length > 0) {
|
|
27
|
+
const service = serviceInfo[0];
|
|
28
|
+
const address = service.ServiceAddress || service.Address;
|
|
29
|
+
const port = service.ServicePort;
|
|
30
|
+
return {
|
|
31
|
+
host: address,
|
|
32
|
+
port: port,
|
|
33
|
+
};
|
|
34
|
+
} else {
|
|
35
|
+
throw new NotFoundException(`Service ${serviceName} not found in Consul catalog`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static async sendTCPMessage(service, messagePattern, payload):Promise<any>{
|
|
40
|
+
const client: ClientProxy = ClientProxyFactory.create({
|
|
41
|
+
transport: Transport.TCP,
|
|
42
|
+
options: await MicroserviceLib.getServiceURI(service),
|
|
43
|
+
});
|
|
44
|
+
try {
|
|
45
|
+
const result = await lastValueFrom(await client.send(messagePattern, payload));
|
|
46
|
+
return result;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw error;
|
|
49
|
+
} finally {
|
|
50
|
+
client.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
21
54
|
private static async registerService(address: net.AddressInfo) {
|
|
22
55
|
const projectName=await this.projectName();
|
|
23
56
|
const serviceData = {
|
|
@@ -33,7 +66,7 @@ export class MicroserviceLib {
|
|
|
33
66
|
}
|
|
34
67
|
};
|
|
35
68
|
|
|
36
|
-
await axios.put(process.env["CONSUL_URL"]
|
|
69
|
+
await axios.put(`${process.env["CONSUL_URL"]}/v1/agent/service/register`, serviceData);
|
|
37
70
|
console.log(`Service registered with Consul on ${address.address}:${address.port}`);
|
|
38
71
|
}
|
|
39
72
|
|