Skip to content

Commit

Permalink
feat(example-express-composition): add code path to allow life cycle …
Browse files Browse the repository at this point in the history
…observers

See #3878
  • Loading branch information
raymondfeng committed Oct 7, 2019
1 parent 2c5a131 commit 8d3401d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 2 additions & 0 deletions examples/express-composition/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ if (require.main === module) {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
// Use the LB4 application as a route. It should not be listening.
listenOnStart: false,
},
};
application.main(config).catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Client} from '@loopback/testlab';
import {setupExpressApplication} from './test-helper';
import {Client, expect} from '@loopback/testlab';
import {HelloObserver} from '../../observers';
import {ExpressServer} from '../../server';
import {setupExpressApplication} from './test-helper';

describe('ExpressApplication', () => {
let server: ExpressServer;
Expand Down Expand Up @@ -58,4 +59,12 @@ describe('ExpressApplication', () => {
.expect(/url\: '\.\/openapi\.json'\,/)
.expect(/<title>LoopBack API Explorer/);
});

it('triggers life cycle start', async () => {
const observer: HelloObserver = await server.lbApp.get(
'lifeCycleObservers.HelloObserver',
);
expect(observer.events.length).to.be.above(0);
expect(observer.events[0]).to.match(/hello-start$/);
});
});
37 changes: 37 additions & 0 deletions examples/express-composition/src/observers/hello.observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-express-composition
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
lifeCycleObserver, // The decorator
LifeCycleObserver,
} from '@loopback/core';

/**
* This class will be bound to the application as a `LifeCycleObserver` during
* `boot`
*/
@lifeCycleObserver()
export class HelloObserver implements LifeCycleObserver {
events: string[] = [];
/*
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE) private app: Application,
) {}
*/

/**
* This method will be invoked when the application starts
*/
async start(): Promise<void> {
this.events.push(`${new Date()}: hello-start`);
}

/**
* This method will be invoked when the application stops
*/
async stop(): Promise<void> {
this.events.push(`${new Date()}: hello-stop`);
}
}
6 changes: 6 additions & 0 deletions examples/express-composition/src/observers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-express-composition
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './hello.observer';
13 changes: 8 additions & 5 deletions examples/express-composition/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {NoteApplication} from './application';
import {ApplicationConfig} from '@loopback/core';
import {Request, Response} from 'express';
import * as express from 'express';
import * as path from 'path';
import pEvent from 'p-event';
import {Request, Response} from 'express';
import * as http from 'http';
import pEvent from 'p-event';
import * as path from 'path';
import {NoteApplication} from './application';

export class ExpressServer {
private app: express.Application;
public readonly lbApp: NoteApplication;
private server: http.Server;
private server?: http.Server;

constructor(options: ApplicationConfig = {}) {
this.app = express();
Expand All @@ -40,6 +40,7 @@ export class ExpressServer {
}

public async start() {
await this.lbApp.start();
const port = this.lbApp.restServer.config.port || 3000;
const host = this.lbApp.restServer.config.host || '127.0.0.1';
this.server = this.app.listen(port, host);
Expand All @@ -49,7 +50,9 @@ export class ExpressServer {
// For testing purposes
public async stop() {
if (!this.server) return;
await this.lbApp.stop();
this.server.close();
await pEvent(this.server, 'close');
this.server = undefined;
}
}

0 comments on commit 8d3401d

Please sign in to comment.