Created
May 8, 2024 17:33
-
-
Save sonthanhdan/4939027f95d9face71cc25bc8508585c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'reflect-metadata'; | |
| import { container } from 'tsyringe'; | |
| import serverlessExpress from '@vendia/serverless-express'; | |
| import { Logger } from '@aws-lambda-powertools/logger'; | |
| import { Tracer } from '@aws-lambda-powertools/tracer'; | |
| import { capturePromise } from 'aws-xray-sdk-core'; | |
| import env from '@/configs'; | |
| import { DbBootstrapper } from '@/database/db-bootstrapper'; | |
| import app from './src/server'; | |
| import type { APIGatewayProxyEvent, APIGatewayProxyResult, Callback, Context, Handler } from 'aws-lambda'; | |
| let serverlessExpressInstance: Handler; | |
| const logger = new Logger({ serviceName: 'service name', logLevel: env.isDev ? 'debug' : 'info' }); | |
| const tracer = new Tracer({ serviceName: 'service name', captureHTTPsRequests: true }); | |
| // capture all promises | |
| capturePromise(); | |
| async function setup( | |
| event: APIGatewayProxyEvent, | |
| context: Context, | |
| callback: Callback<APIGatewayProxyResult>, | |
| ): Promise<APIGatewayProxyResult> { | |
| // register logger and tracer | |
| container.registerInstance(Logger, logger); | |
| container.registerInstance(Tracer, tracer); | |
| // init db | |
| DbBootstrapper.initialize(); | |
| serverlessExpressInstance = serverlessExpress({ app }); | |
| return serverlessExpressInstance(event, context, callback); | |
| } | |
| export async function serve( | |
| event: APIGatewayProxyEvent, | |
| context: Context, | |
| callback: Callback<APIGatewayProxyResult>, | |
| ): Promise<APIGatewayProxyResult> { | |
| if (serverlessExpressInstance) return serverlessExpressInstance(event, context, callback); | |
| return setup(event, context, callback); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment