@haku-sci/utils 1.2.5 → 1.2.6
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/package.json
CHANGED
package/utils/src/index.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { INestMicroservice, Injectable, NotFoundException } from '@nestjs/common';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import { NestFactory } from '@nestjs/core';
|
|
5
|
-
import { ClientProxy, ClientProxyFactory, MicroserviceOptions, Transport } from '@nestjs/microservices';
|
|
6
|
-
import * as net from 'net';
|
|
7
|
-
import axios from 'axios';
|
|
8
|
-
import { lastValueFrom } from 'rxjs';
|
|
9
|
-
|
|
10
|
-
@Injectable()
|
|
11
|
-
export class MicroserviceService {
|
|
12
|
-
static async projectName(): Promise<string> {
|
|
13
|
-
var data = await readFile(
|
|
14
|
-
path.join(
|
|
15
|
-
process.cwd(),
|
|
16
|
-
'package.json',
|
|
17
|
-
)
|
|
18
|
-
)
|
|
19
|
-
const { name } = JSON.parse(data.toString());
|
|
20
|
-
return name;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private static async getServiceURI(serviceName: string) :Promise<{host: string, port: number}> {
|
|
24
|
-
const response = await axios.get(`${process.env["CONSUL_URL"]}/v1/catalog/service/${serviceName}`);
|
|
25
|
-
const serviceInfo = response.data;
|
|
26
|
-
|
|
27
|
-
if (serviceInfo.length > 0) {
|
|
28
|
-
const service = serviceInfo[0];
|
|
29
|
-
const address = service.ServiceAddress || service.Address;
|
|
30
|
-
const port = service.ServicePort;
|
|
31
|
-
return {
|
|
32
|
-
host: address,
|
|
33
|
-
port: port,
|
|
34
|
-
};
|
|
35
|
-
} else {
|
|
36
|
-
throw new NotFoundException(`Service ${serviceName} not found in Consul catalog`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static async sendTCPMessage(service, messagePattern, payload?):Promise<any>{
|
|
41
|
-
const client: ClientProxy = ClientProxyFactory.create({
|
|
42
|
-
transport: Transport.TCP,
|
|
43
|
-
options: await MicroserviceService.getServiceURI(service),
|
|
44
|
-
});
|
|
45
|
-
try {
|
|
46
|
-
const result = await lastValueFrom(await client.send(messagePattern, payload));
|
|
47
|
-
return result;
|
|
48
|
-
} catch (error) {
|
|
49
|
-
throw error;
|
|
50
|
-
} finally {
|
|
51
|
-
client.close();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private static async registerService(address: net.AddressInfo) {
|
|
56
|
-
const projectName=await this.projectName();
|
|
57
|
-
const serviceData = {
|
|
58
|
-
ID: projectName,
|
|
59
|
-
Name: projectName,
|
|
60
|
-
Address: address.address,
|
|
61
|
-
Port: address.port,
|
|
62
|
-
Check: {
|
|
63
|
-
TCP: `${address.address}:${address.port}`,
|
|
64
|
-
Interval: "10s",
|
|
65
|
-
Timeout: "5s",
|
|
66
|
-
DeregisterCriticalServiceAfter: "1m"
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
await axios.put(`${process.env["CONSUL_URL"]}/v1/agent/service/register`, serviceData);
|
|
71
|
-
console.log(`Service registered with Consul on ${address.address}:${address.port}`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
static async bootstrapMicroservice(appModule):Promise<INestMicroservice> {
|
|
75
|
-
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
|
|
76
|
-
appModule,
|
|
77
|
-
{
|
|
78
|
-
transport: Transport.TCP,
|
|
79
|
-
},
|
|
80
|
-
);
|
|
81
|
-
await app.listen();
|
|
82
|
-
if (process.env["CONSUL_URL"]){
|
|
83
|
-
const tcpServer: net.Server = (app as any).server.server;
|
|
84
|
-
this.registerService(tcpServer.address() as net.AddressInfo)
|
|
85
|
-
}
|
|
86
|
-
return app
|
|
87
|
-
}
|
|
88
|
-
}
|