From aaea24432614e48b47f41b292d64eeaae4280a36 Mon Sep 17 00:00:00 2001 From: Livio Brunner Date: Mon, 16 Dec 2019 10:40:09 +0100 Subject: [PATCH] fix(): Do not bootstrap terminus in case no http server Fixes #461 --- lib/terminus-bootstrap.service.spec.ts | 29 ++++++++++++++++++++++++++ lib/terminus-bootstrap.service.ts | 26 ++++++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/terminus-bootstrap.service.spec.ts b/lib/terminus-bootstrap.service.spec.ts index 581fafecb..77c4099a7 100644 --- a/lib/terminus-bootstrap.service.spec.ts +++ b/lib/terminus-bootstrap.service.spec.ts @@ -81,6 +81,35 @@ describe('TerminusBootstrapService', () => { // httpAdapterHost = module.get(HttpAdapterHost); }); describe('onApplicationBootstrap', () => { + it('should ignore bootstrap if there is no HTTP Server', async () => { + const module = Test.createTestingModule({ + providers: [ + TerminusBootstrapService, + { + provide: TERMINUS_MODULE_OPTIONS, + useValue: options, + }, + { + provide: TERMINUS_LIB, + useValue: terminusMock, + }, + { + provide: HttpAdapterHost, + useValue: null, + }, + { + provide: ApplicationConfig, + useValue: applicationConfigMock, + }, + ], + }); + const context = await module.compile(); + const bootstrapService = context.get(TerminusBootstrapService); + const terminus = context.get(TERMINUS_LIB); + + bootstrapService.onApplicationBootstrap(); + expect(terminus).not.toHaveBeenCalled(); + }); it('should call the terminus correctly on application bootstrap', () => { expect(terminus).not.toHaveBeenCalled(); diff --git a/lib/terminus-bootstrap.service.ts b/lib/terminus-bootstrap.service.ts index 709ee1e60..b19b1050c 100644 --- a/lib/terminus-bootstrap.service.ts +++ b/lib/terminus-bootstrap.service.ts @@ -145,11 +145,14 @@ export class TerminusBootstrapService implements OnApplicationBootstrap { * indicators */ public getHealthChecksMap(): HealthCheckMap { - return this.options.endpoints.reduce((healthChecks, endpoint) => { - const url = this.validateEndpointUrl(endpoint); - healthChecks[url] = this.getHealthCheckExecutor(endpoint); - return healthChecks; - }, {} as HealthCheckMap); + return this.options.endpoints.reduce( + (healthChecks, endpoint) => { + const url = this.validateEndpointUrl(endpoint); + healthChecks[url] = this.getHealthCheckExecutor(endpoint); + return healthChecks; + }, + {} as HealthCheckMap, + ); } /** @@ -169,11 +172,20 @@ export class TerminusBootstrapService implements OnApplicationBootstrap { this.logHealthCheckRegister(healthChecks); } + private hasHttpServer(): boolean { + return this.refHost && this.refHost.httpAdapter && this.refHost.httpAdapter; + } + /** * Gets called when the application gets bootstrapped. */ public onApplicationBootstrap() { - this.httpServer = this.refHost.httpAdapter.getHttpServer(); - this.bootstrapTerminus(); + // In case the application context has been bootstrapped with + // NestFactory.createApplicationContext(), ignore bootstrapping + // Terminus + if (this.hasHttpServer()) { + this.httpServer = this.refHost.httpAdapter.getHttpServer(); + this.bootstrapTerminus(); + } } }