This commit is contained in:
vzaytsev 2024-11-15 13:27:40 +03:00
parent a03119b692
commit 1c6c3d38fb
4 changed files with 74 additions and 0 deletions

16
src/application.ts Normal file
View File

@ -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',
});
}
}

View File

@ -0,0 +1,9 @@
import {get} from '@loopback/rest';
export class PingController {
@get('/ping')
ping(): object {
return {message: 'pong'};
}
}

14
src/index.ts Normal file
View File

@ -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);
});

35
src/sequence.ts Normal file
View File

@ -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<void> {
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);
}
}
}