diff --git a/src/application.ts b/src/application.ts new file mode 100644 index 0000000..2a642d5 --- /dev/null +++ b/src/application.ts @@ -0,0 +1,16 @@ +import {ApplicationConfig} from '@loopback/core'; +import {RestApplication} from '@loopback/rest'; +import {RestExplorerBindings} from '@loopback/rest-explorer'; +import {PingController} from './controllers/ping.controller'; + +export class Application extends RestApplication { + constructor(options: ApplicationConfig = {}) { + super(options); + this.projectRoot = __dirname; + this.controller(PingController); + this.bind(RestExplorerBindings.CONFIG).to({ + path: '/explorer', + }); + } +} + diff --git a/src/controllers/ping.controller.ts b/src/controllers/ping.controller.ts new file mode 100644 index 0000000..7b76301 --- /dev/null +++ b/src/controllers/ping.controller.ts @@ -0,0 +1,9 @@ +import {get} from '@loopback/rest'; + +export class PingController { + @get('/ping') + ping(): object { + return {message: 'pong'}; + } +} + diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..7c0b431 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,14 @@ +import {Application} from './application'; + +export async function main() { + const app = new Application(); + await app.boot(); + await app.start(); + console.log(`Application is running at http://127.0.0.1:3000`); +} + +main().catch(err => { + console.error('Cannot start the application.', err); + process.exit(1); +}); + diff --git a/src/sequence.ts b/src/sequence.ts new file mode 100644 index 0000000..1712299 --- /dev/null +++ b/src/sequence.ts @@ -0,0 +1,35 @@ +import { + SequenceHandler, + FindRoute, + ParseParams, + InvokeMethod, + Send, + Reject, + RequestContext, +} from '@loopback/rest'; + +export class MySequence implements SequenceHandler { + constructor( + public findRoute: FindRoute, + public parseParams: ParseParams, + public invoke: InvokeMethod, + public send: Send, + public reject: Reject, + ) {} + + async handle(context: RequestContext): Promise { + const {request, response} = context; + try { + 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); + } + } +} +