Skip to content

Commit

Permalink
Merge pull request #465 from nestjs/fix/issue-461
Browse files Browse the repository at this point in the history
fix(): Do not bootstrap terminus in case no http server
  • Loading branch information
BrunnerLivio authored Dec 16, 2019
2 parents e0b6076 + aaea244 commit ab3fd97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
29 changes: 29 additions & 0 deletions lib/terminus-bootstrap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
26 changes: 19 additions & 7 deletions lib/terminus-bootstrap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

/**
Expand All @@ -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();
}
}
}

0 comments on commit ab3fd97

Please sign in to comment.