Skip to content

Commit

Permalink
fix: improve home page controller with di of pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Sep 25, 2018
1 parent af2db9d commit 7cfc221
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
4 changes: 3 additions & 1 deletion public/index.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
</head>

<body>
<h1 style="text-align: center">${name}@${version}</h1>
<h1 style="text-align: center">${name}</h1>
<p style="text-align: center">Version ${version}</p>
<br/>
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h4>
<h3>API Explorer: <a href="/explorer">/explorer</a></h4>

Expand Down
17 changes: 16 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {ApplicationConfig} from '@loopback/core';
import {ApplicationConfig, BindingKey} from '@loopback/core';
import {RestApplication} from '@loopback/rest';
import {MySequence} from './sequence';

Expand All @@ -20,12 +20,27 @@ import {
} from '@loopback/repository';
/* tslint:enable:no-unused-variable */

/**
* Information from package.json
*/
export interface PackageInfo {
name: string;
version: string;
description: string;
}
export const PackageKey = BindingKey.create<PackageInfo>('application.package');

const pkg: PackageInfo = require('../../package.json');

export class ShoppingApplication extends BootMixin(
RepositoryMixin(RestApplication),
) {
constructor(options?: ApplicationConfig) {
super(options);

// Bind package.json to the application context
this.bind(PackageKey).to(pkg);

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

Expand Down
18 changes: 7 additions & 11 deletions src/controllers/home-page.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import * as path from 'path';
import {template, TemplateExecutor} from 'lodash';
import {inject} from '@loopback/context';
import {RestBindings, Response} from '@loopback/rest';

const pkg = require('../../../package.json');
import {PackageInfo, PackageKey} from '../application';

export class HomePageController {
name: string;
version: string;
description: string;
render: TemplateExecutor;

constructor(@inject(RestBindings.Http.RESPONSE) private res: Response) {
this.name = pkg.name;
this.version = pkg.version;
this.description = pkg.description || pkg.name;
constructor(
@inject(PackageKey) private pkg: PackageInfo,
@inject(RestBindings.Http.RESPONSE) private response: Response,
) {
const html = fs.readFileSync(
path.join(__dirname, '../../../public/index.html.template'),
'utf-8',
Expand All @@ -38,8 +34,8 @@ export class HomePageController {
},
})
homePage() {
const homePage = this.render(this);
this.res
const homePage = this.render(this.pkg);
this.response
.status(200)
.contentType('html')
.send(homePage);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {ShoppingApplication} from './application';
import {ApplicationConfig} from '@loopback/core';

export {ShoppingApplication};
export {ShoppingApplication, PackageInfo, PackageKey} from './application';

export async function main(options?: ApplicationConfig) {
const app = new ShoppingApplication(options);
Expand Down
51 changes: 51 additions & 0 deletions test/acceptance/home-page.controller.acceptance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-shopping
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {ShoppingApplication} from '../..';

describe('HomePageController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;

before(givenAnApplication);

before(givenARestServer);

before(async () => {
await app.boot();
await app.start();
});

before(() => {
client = createClientForHandler(server.requestHandler);
});

after(async () => {
await app.stop();
});

it('exposes a default home page', async () => {
const res = await client
.get('/')
.expect(200)
.expect('Content-Type', /text\/html/);
expect(res.body).to.match(/@loopback\/example\-shopping/);
});

function givenAnApplication() {
app = new ShoppingApplication({
rest: {
port: 0,
},
});
}

async function givenARestServer() {
server = await app.getServer(RestServer);
}
});

0 comments on commit 7cfc221

Please sign in to comment.