-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
nest-microservice.ts
183 lines (158 loc) 路 5.42 KB
/
nest-microservice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
CanActivate,
ExceptionFilter,
INestMicroservice,
NestInterceptor,
PipeTransform,
WebSocketAdapter,
} from '@nestjs/common';
import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
import { Logger } from '@nestjs/common/services/logger.service';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { MESSAGES } from '@nestjs/core/constants';
import { optionalRequire } from '@nestjs/core/helpers/optional-require';
import { NestContainer } from '@nestjs/core/injector/container';
import { NestApplicationContext } from '@nestjs/core/nest-application-context';
import { Transport } from './enums/transport.enum';
import { CustomTransportStrategy } from './interfaces/custom-transport-strategy.interface';
import { MicroserviceOptions } from './interfaces/microservice-configuration.interface';
import { MicroservicesModule } from './microservices-module';
import { Server } from './server/server';
import { ServerFactory } from './server/server-factory';
const { SocketModule } = optionalRequire(
'@nestjs/websockets/socket-module',
() => require('@nestjs/websockets/socket-module'),
);
export class NestMicroservice
extends NestApplicationContext
implements INestMicroservice
{
private readonly logger = new Logger(NestMicroservice.name, {
timestamp: true,
});
private readonly microservicesModule = new MicroservicesModule();
private readonly socketModule = SocketModule ? new SocketModule() : null;
private microserviceConfig: NestMicroserviceOptions & MicroserviceOptions;
private server: Server & CustomTransportStrategy;
private isTerminated = false;
private isInitHookCalled = false;
constructor(
container: NestContainer,
config: NestMicroserviceOptions & MicroserviceOptions = {},
private readonly applicationConfig: ApplicationConfig,
) {
super(container);
this.microservicesModule.register(container, this.applicationConfig);
this.createServer(config);
this.selectContextModule();
}
public createServer(config: NestMicroserviceOptions & MicroserviceOptions) {
try {
this.microserviceConfig = {
transport: Transport.TCP,
...config,
} as any;
const { strategy } = config as any;
this.server = strategy
? strategy
: ServerFactory.create(this.microserviceConfig);
} catch (e) {
this.logger.error(e);
throw e;
}
}
public async registerModules(): Promise<any> {
this.socketModule &&
this.socketModule.register(this.container, this.applicationConfig);
this.microservicesModule.setupClients(this.container);
this.registerListeners();
this.setIsInitialized(true);
if (!this.isInitHookCalled) {
await this.callInitHook();
await this.callBootstrapHook();
}
}
public registerListeners() {
this.microservicesModule.setupListeners(this.container, this.server);
}
public useWebSocketAdapter(adapter: WebSocketAdapter): this {
this.applicationConfig.setIoAdapter(adapter);
return this;
}
public useGlobalFilters(...filters: ExceptionFilter[]): this {
this.applicationConfig.useGlobalFilters(...filters);
return this;
}
public useGlobalPipes(...pipes: PipeTransform<any>[]): this {
this.applicationConfig.useGlobalPipes(...pipes);
return this;
}
public useGlobalInterceptors(...interceptors: NestInterceptor[]): this {
this.applicationConfig.useGlobalInterceptors(...interceptors);
return this;
}
public useGlobalGuards(...guards: CanActivate[]): this {
this.applicationConfig.useGlobalGuards(...guards);
return this;
}
public async init(): Promise<this> {
if (this.isInitialized) {
return this;
}
await super.init();
await this.registerModules();
return this;
}
public async listen() {
!this.isInitialized && (await this.registerModules());
return new Promise<any>((resolve, reject) => {
this.server.listen((err, info) => {
if (this.microserviceConfig?.autoFlushLogs ?? true) {
this.flushLogs();
}
if (err) {
return reject(err);
}
this.logger.log(MESSAGES.MICROSERVICE_READY);
resolve(info);
});
});
}
public async listenAsync(): Promise<any> {
this.logger.warn(
'DEPRECATED! "listenAsync" method is deprecated and will be removed in the next major release. Please, use "listen" instead.',
);
return this.listen();
}
public async close(): Promise<any> {
await this.server.close();
if (this.isTerminated) {
return;
}
this.setIsTerminated(true);
await this.closeApplication();
}
public setIsInitialized(isInitialized: boolean) {
this.isInitialized = isInitialized;
}
public setIsTerminated(isTerminated: boolean) {
this.isTerminated = isTerminated;
}
public setIsInitHookCalled(isInitHookCalled: boolean) {
this.isInitHookCalled = isInitHookCalled;
}
protected async closeApplication(): Promise<any> {
this.socketModule && (await this.socketModule.close());
this.microservicesModule && (await this.microservicesModule.close());
await super.close();
this.setIsTerminated(true);
}
protected async dispose(): Promise<void> {
await this.server.close();
if (this.isTerminated) {
return;
}
this.socketModule && (await this.socketModule.close());
this.microservicesModule && (await this.microservicesModule.close());
}
}