Arundathi Patil | 85db5b0 | 2019-01-17 14:10:08 +0530 | [diff] [blame] | 1 | /* |
| 2 | ============LICENSE_START========================================== |
| 3 | =================================================================== |
| 4 | Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved. |
| 5 | =================================================================== |
| 6 | |
| 7 | Unless otherwise specified, all software contained herein is licensed |
| 8 | under the Apache License, Version 2.0 (the License); |
| 9 | you may not use this software except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | ============LICENSE_END============================================ |
| 20 | */ |
| 21 | |
| 22 | import { Request, RestBindings, get, ResponseObject } from '@loopback/rest'; |
| 23 | import { inject } from '@loopback/context'; |
| 24 | |
| 25 | /** |
| 26 | * OpenAPI response for ping() |
| 27 | */ |
| 28 | const PING_RESPONSE: ResponseObject = { |
| 29 | description: 'Ping Response', |
| 30 | content: { |
| 31 | 'application/json': { |
| 32 | schema: { |
| 33 | type: 'object', |
| 34 | properties: { |
| 35 | greeting: { type: 'string' }, |
| 36 | date: { type: 'string' }, |
| 37 | url: { type: 'string' }, |
| 38 | headers: { |
| 39 | type: 'object', |
| 40 | properties: { |
| 41 | 'Content-Type': { type: 'string' }, |
| 42 | }, |
| 43 | additionalProperties: true, |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | }, |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * A simple controller to bounce back http requests |
| 53 | */ |
| 54 | export class PingController { |
| 55 | constructor(@inject(RestBindings.Http.REQUEST) private req: Request) { } |
| 56 | |
| 57 | // Map to `GET /ping` |
| 58 | @get('/ping', { |
| 59 | responses: { |
| 60 | '200': PING_RESPONSE, |
| 61 | }, |
| 62 | }) |
| 63 | ping(): object { |
| 64 | // Reply with a greeting, the current time, the url, and request headers |
| 65 | return { |
| 66 | greeting: 'Hello from LoopBack', |
| 67 | date: new Date(), |
| 68 | url: this.req.url, |
| 69 | headers: Object.assign({}, this.req.headers), |
| 70 | }; |
| 71 | } |
| 72 | } |