Skip to content

Commit

Permalink
feat(example-todo): add morgan middleware if env var DEBUG is set
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 16, 2020
1 parent d7735e4 commit e1171af
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 11 deletions.
157 changes: 150 additions & 7 deletions examples/todo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@loopback/rest-explorer": "^2.0.4",
"@loopback/service-proxy": "^2.0.4",
"loopback-connector-rest": "^3.6.0",
"morgan": "^1.10.0",
"tslib": "^1.11.1"
},
"devDependencies": {
Expand All @@ -53,6 +54,7 @@
"@loopback/http-caching-proxy": "^2.0.4",
"@loopback/testlab": "^3.0.1",
"@types/lodash": "^4.14.149",
"@types/morgan": "^1.9.0",
"@types/node": "^10.17.19",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
Expand Down
13 changes: 13 additions & 0 deletions examples/todo/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {RestExplorerComponent} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
import morgan from 'morgan';
import path from 'path';
import {MySequence} from './sequence';

Expand All @@ -18,6 +19,18 @@ export class TodoListApplication extends BootMixin(
constructor(options: ApplicationConfig = {}) {
super(options);

if (process.env.DEBUG) {
// Register `morgan` express middleware
// Create a middleware factory wrapper for `morgan(format, options)`
const morganFactory = (config?: morgan.Options) =>
morgan('combined', config);
this.middleware(
morganFactory,
{},
{injectConfiguration: false, key: 'middleware.morgan'},
);
}

// Set up the custom sequence
this.sequence(MySequence);

Expand Down
16 changes: 12 additions & 4 deletions examples/todo/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, inject} from '@loopback/context';
import {inject} from '@loopback/context';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,8 +19,14 @@ import {
const SequenceActions = RestBindings.SequenceActions;

export class MySequence implements SequenceHandler {
/**
* Optional invoker for registered middleware in a chain.
* To be injected via SequenceActions.INVOKE_MIDDLEWARE.
*/
@inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true})
protected invokeMiddleware: InvokeMiddleware = () => {};

constructor(
@inject(RestBindings.Http.CONTEXT) public ctx: Context,
@inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
Expand All @@ -30,12 +37,13 @@ export class MySequence implements SequenceHandler {
async handle(context: RequestContext) {
try {
const {request, response} = context;
await this.invokeMiddleware(context);
const route = this.findRoute(request);
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(response, result);
} catch (error) {
this.reject(context, error);
} catch (err) {
this.reject(context, err);
}
}
}

0 comments on commit e1171af

Please sign in to comment.