Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a default home page #16

Merged
merged 7 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 86 additions & 274 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,31 @@
"src"
],
"dependencies": {
"@loopback/boot": "^0.14.0",
"@loopback/context": "^0.12.8",
"@loopback/core": "^0.11.9",
"@loopback/boot": "^0.14.1",
"@loopback/context": "^0.12.11",
"@loopback/core": "^0.11.12",
"@loopback/dist-util": "^0.3.7",
"@loopback/openapi-v3": "^0.14.2",
"@loopback/repository": "^0.17.1",
"@loopback/rest": "^0.24.0",
"@loopback/openapi-v3": "^0.15.0",
"@loopback/repository": "^0.18.0",
"@loopback/rest": "^0.25.0",
"bcryptjs": "^2.4.3",
"debug": "^4.0.1",
"isemail": "^3.1.3",
"lodash": "^4.17.11",
"loopback-connector-kv-redis": "^3.0.0",
"loopback-connector-mongodb": "^3.7.1"
"loopback-connector-mongodb": "^3.8.0"
},
"devDependencies": {
"@commitlint/cli": "^7.1.2",
"@commitlint/config-conventional": "^7.1.2",
"@commitlint/travis-cli": "^7.1.2",
"@loopback/build": "^0.7.3",
"@loopback/testlab": "^0.13.0",
"@types/bcryptjs": "^2.4.1",
"@types/debug": "0.0.30",
"@loopback/testlab": "^0.14.0",
"@types/bcryptjs": "^2.4.2",
"@types/debug": "^0.0.30",
"@types/lodash": "^4.14.116",
"@types/mocha": "^5.0.0",
"@types/node": "^10.9.4",
"@types/node": "^10.11.0",
"commitizen": "^2.10.1",
"cz-conventional-changelog": "^2.1.0",
"husky": "^0.14.3",
Expand Down
52 changes: 52 additions & 0 deletions public/index.html.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>${description}</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
h3 {
margin-left: 25px;
text-align: center;
}

a, a:visited {
color: #3f5dff;
}

h3 a {
margin-left: 10px;
}

a:hover, a:focus, a:active {
color: #001956;
}

.power {
position: absolute;
bottom: 25px;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>

<body>
<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>

<footer class="power">
<a href="https://v4.loopback.io" target="_blank">
<img src="https://loopback.io/images/branding/powered-by-loopback/blue/powered-by-loopback-sm.png" />
</a>
</footer>
</body>

</html>
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
43 changes: 43 additions & 0 deletions src/controllers/home-page.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 {get} from '@loopback/openapi-v3';
import * as fs from 'fs';
import * as path from 'path';
import {template, TemplateExecutor} from 'lodash';
import {inject} from '@loopback/context';
import {RestBindings, Response} from '@loopback/rest';
import {PackageInfo, PackageKey} from '../application';

export class HomePageController {
render: TemplateExecutor;

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',
);
this.render = template(html);
}

@get('/', {
responses: {
'200': {
description: 'Home Page',
content: {'text/html': {schema: {type: 'string'}}},
},
},
})
homePage() {
const homePage = this.render(this.pkg);
this.response
.status(200)
.contentType('html')
.send(homePage);
}
}
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
export * from './ping.controller';
export * from './user.controller';
export * from './shopping-cart.controller';
export * from './home-page.controller';
37 changes: 34 additions & 3 deletions src/controllers/ping.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Request, RestBindings} from '@loopback/rest';
import {get} from '@loopback/openapi-v3';
import {Request, RestBindings, get, ResponseObject} from '@loopback/rest';
import {inject} from '@loopback/context';
import {} from '@loopback/openapi-v3-types';

/**
* OpenAPI response for ping()
*/
const PING_RESPONSE: ResponseObject = {
description: 'Ping Response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
greeting: {type: 'string'},
date: {type: 'string'},
url: {type: 'string'},
headers: {
type: 'object',
patternProperties: {
'^.*$': {type: 'string'},
},
additionalProperties: false,
},
},
},
},
},
};

/**
* A simple controller to bounce back http requests
Expand All @@ -14,8 +40,13 @@ export class PingController {
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {}

// Map to `GET /ping`
@get('/ping')
@get('/ping', {
responses: {
'200': PING_RESPONSE,
},
})
ping(): object {
// Reply with a greeting, the current time, the url, and request headers
return {
greeting: 'Hello from LoopBack',
date: new Date(),
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, Client, expect} from '@loopback/testlab';
import {ShoppingApplication} from '../..';
import {RestServer} from '@loopback/rest';

describe('HomePageController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: Client;

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);
}
});
7 changes: 4 additions & 3 deletions test/acceptance/ping.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

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

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

before(givenAnApplication);

Expand All @@ -30,7 +30,8 @@ describe('PingController', () => {
});

it('invokes GET /ping', async () => {
await client.get('/ping?msg=world').expect(200);
const res = await client.get('/ping?msg=world').expect(200);
expect(res.body).to.containEql({greeting: 'Hello from LoopBack'});
});

function givenAnApplication() {
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/shopping-cart.controller.acceptance.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 {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {createClientForHandler, Client, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {ShoppingApplication} from '../..';
import {ShoppingCartRepository} from '../../src/repositories';
Expand All @@ -13,7 +13,7 @@ import {ShoppingCart, ShoppingCartItem} from '../../src/models';
describe('ShoppingCartController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
const cartRepo = new ShoppingCartRepository(new RedisDataSource());

before(givenAnApplication);
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/user.controller.acceptance.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 {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {createClientForHandler, Client, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {ShoppingApplication} from '../..';
import {UserRepository} from '../../src/repositories';
Expand All @@ -12,7 +12,7 @@ import {UserDataSource} from '../../src/datasources';
describe('UserController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
const userRepo = new UserRepository(new UserDataSource());

const user = {
Expand Down