forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnest-application.ts
More file actions
159 lines (130 loc) · 5.48 KB
/
Copy pathnest-application.ts
File metadata and controls
159 lines (130 loc) · 5.48 KB
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
import iterate from 'iterare';
import { MiddlewaresModule } from './middlewares/middlewares-module';
import { SocketModule } from '@nestjs/websockets/socket-module';
import { NestContainer } from './injector/container';
import { ExpressAdapter } from './adapters/express-adapter';
import { RoutesResolver } from './router/routes-resolver';
import { Logger } from '@nestjs/common/services/logger.service';
import { messages } from './constants';
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
import { Resolver } from './router/interfaces/resolver.interface';
import { INestApplication, INestMicroservice, OnModuleInit } from '@nestjs/common';
import { ApplicationConfig } from './application-config';
import { validatePath, isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { MicroserviceConfiguration } from '@nestjs/microservices';
import { NestMicroservice } from './index';
import { WebSocketAdapter, OnModuleDestroy, ExceptionFilter, PipeTransform } from '@nestjs/common';
import { Module } from './injector/module';
export class NestApplication implements INestApplication {
private readonly config = new ApplicationConfig();
private readonly logger = new Logger(NestApplication.name);
private readonly routesResolver: Resolver = null;
private readonly microservices = [];
private isInitialized = false;
private server = null;
constructor(
private readonly container: NestContainer,
private readonly express) {
this.routesResolver = new RoutesResolver(
container, ExpressAdapter, this.config,
);
}
public async setupModules() {
SocketModule.setup(this.container, this.config);
MicroservicesModule.setup(this.container);
MicroservicesModule.setupClients(this.container);
await MiddlewaresModule.setup(this.container, this.config);
}
public async init() {
await this.setupModules();
const router = ExpressAdapter.createRouter();
await this.setupMiddlewares(router);
this.setupRoutes(router);
this.express.use(validatePath(this.config.getGlobalPrefix()), router);
this.callInitHook();
this.logger.log(messages.APPLICATION_READY);
this.isInitialized = true;
}
public connectMicroservice(config: MicroserviceConfiguration): INestMicroservice {
const instance = new NestMicroservice(this.container, config);
instance.setupListeners();
instance.setIsInitialized(true);
this.microservices.push(instance);
return instance;
}
public getMicroservices(): INestMicroservice[] {
return this.microservices;
}
public startAllMicroservices(callback: () => void) {
Promise.all(this.microservices.map(this.listenToPromise)).then(callback);
}
public async listen(port: number, callback?: () => void) {
(!this.isInitialized) && await this.init();
this.server = this.express.listen(port, callback);
return this.server;
}
public close() {
SocketModule.close();
this.server && this.server.close();
this.microservices.forEach((microservice) => {
microservice.setIsTerminated(true);
microservice.close();
});
this.callDestroyHook();
}
public setGlobalPrefix(prefix: string) {
this.config.setGlobalPrefix(prefix);
}
public useWebSocketAdapter(adapter: WebSocketAdapter) {
this.config.setIoAdapter(adapter);
}
public useGlobalFilters(...filters: ExceptionFilter[]) {
this.config.useGlobalFilters(...filters);
}
public useGlobalPipes(...pipes: PipeTransform[]) {
this.config.useGlobalPipes(...pipes);
}
private async setupMiddlewares(instance) {
await MiddlewaresModule.setupMiddlewares(instance);
}
private setupRoutes(instance) {
this.routesResolver.resolve(instance);
}
private listenToPromise(microservice: INestMicroservice) {
return new Promise((resolve, reject) => {
microservice.listen(resolve);
});
}
private callInitHook() {
const modules = this.container.getModules();
modules.forEach((module) => {
this.callModuleInitHook(module);
});
}
private callModuleInitHook(module: Module) {
const components = [...module.routes, ...module.components];
iterate(components).map(([key, {instance}]) => instance)
.filter((instance) => !isNil(instance))
.filter(this.hasOnModuleInitHook)
.forEach((instance) => (instance as OnModuleInit).onModuleInit());
}
private hasOnModuleInitHook(instance): instance is OnModuleInit {
return !isUndefined((instance as OnModuleInit).onModuleInit);
}
private callDestroyHook() {
const modules = this.container.getModules();
modules.forEach((module) => {
this.callModuleDestroyHook(module);
});
}
private callModuleDestroyHook(module: Module) {
const components = [...module.routes, ...module.components];
iterate(components).map(([key, {instance}]) => instance)
.filter((instance) => !isNil(instance))
.filter(this.hasOnModuleDestroyHook)
.forEach((instance) => (instance as OnModuleDestroy).onModuleDestroy());
}
private hasOnModuleDestroyHook(instance): instance is OnModuleDestroy {
return !isUndefined((instance as OnModuleDestroy).onModuleDestroy);
}
}