diff --git a/packages/deployment-service/cdk/lib/cdk-stack.ts b/packages/deployment-service/cdk/lib/cdk-stack.ts index 1b4e0032a3..733e3c3cbc 100644 --- a/packages/deployment-service/cdk/lib/cdk-stack.ts +++ b/packages/deployment-service/cdk/lib/cdk-stack.ts @@ -152,7 +152,7 @@ export const createStack = async () => { entrypoint: 'bundle/sns.zip', }, httpApi: { - handler: createFileLoc('http', 'handler'), + handler: createFileLoc('httpApi', 'handler'), policies: [...policies.commonBackendPolicies, policies.cloudFrontPolicy, policies.route53Policy], api: { routes: [ diff --git a/packages/deployment-service/src/http.ts b/packages/deployment-service/src/http.ts index cdf656ab5d..890dbc4b53 100644 --- a/packages/deployment-service/src/http.ts +++ b/packages/deployment-service/src/http.ts @@ -46,7 +46,5 @@ async function bootstrapServer(): Promise { export const handler: Handler = async (event: APIGatewayEvent, context: Context) => { cachedServer = await bootstrapServer() - event.path = event.path?.replace('api/', '') - return proxy(cachedServer, event, context, 'PROMISE').promise } diff --git a/packages/deployment-service/src/httpApi.ts b/packages/deployment-service/src/httpApi.ts new file mode 100644 index 0000000000..8a5448d8ee --- /dev/null +++ b/packages/deployment-service/src/httpApi.ts @@ -0,0 +1,60 @@ +import { NestFactory } from '@nestjs/core' +import { INestApplication, ValidationPipe } from '@nestjs/common' +import { AppModule } from './app-module' +import { ExpressAdapter } from '@nestjs/platform-express' +import { createServer, proxy } from 'aws-serverless-express' +import { Handler, Context, APIGatewayEvent } from 'aws-lambda' +import { eventContext } from 'aws-serverless-express/middleware' +import { Server } from 'http' +import express, { Express } from 'express' +import * as bodyParser from 'body-parser' +import { CorsHeaderInterceptor } from '@reapit/utils-nest' +import { APIGatewayProxyResult } from 'aws-lambda/trigger/api-gateway-proxy' +const crypto = require('crypto') + +global.crypto = crypto + +export const bootstrapApplication = async (cors: boolean = false): Promise<[INestApplication, Express]> => { + const expressApp = express() + const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp), { cors }) + + app.useGlobalPipes(new ValidationPipe()) + app.useGlobalInterceptors(new CorsHeaderInterceptor()) + app.use(bodyParser.json({ limit: '50mb' })) + app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })) + + return [app, expressApp] +} + +const binaryMimeTypes: string[] = [] + +let cachedServer: Server + +async function bootstrapServer(): Promise { + if (!cachedServer) { + const [app, express] = await bootstrapApplication() + + app.use(eventContext()) + + await app.init() + + cachedServer = createServer(express, undefined, binaryMimeTypes) + } + + return cachedServer +} + +export const handler: Handler = async (event, context: Context) => { + cachedServer = await bootstrapServer() + + if (!event.path.includes('github')) { + return { + statusCode: 404, + body: 'Not Found', + } + } + + event.path = event.path?.replace('api/', '') + + return proxy(cachedServer, event, context, 'PROMISE').promise +} diff --git a/packages/deployment-service/tsup-zip.config.json b/packages/deployment-service/tsup-zip.config.json index 3c514a50c8..0c96a731ba 100644 --- a/packages/deployment-service/tsup-zip.config.json +++ b/packages/deployment-service/tsup-zip.config.json @@ -1,3 +1,3 @@ { - "bundleFiles": ["dist/http.js", "dist/sqs.js", "dist/sns.js", "dist/migration-run.js", "dist/dns-eventbridge.js"] + "bundleFiles": ["dist/http.js", "dist/httpApi.js", "dist/sqs.js", "dist/sns.js", "dist/migration-run.js", "dist/dns-eventbridge.js"] } diff --git a/packages/deployment-service/tsup.config.js b/packages/deployment-service/tsup.config.js index 3a209e8d11..6b1047835c 100644 --- a/packages/deployment-service/tsup.config.js +++ b/packages/deployment-service/tsup.config.js @@ -7,7 +7,7 @@ const pkgJson = JSON.parse(fs.readFileSync('package.json', 'utf-8')) export default defineConfig([ { - entry: ['src/http.ts', 'src/sqs.ts', 'src/sns.ts', 'src/migration-run.ts', 'src/dns-eventbridge.ts'], + entry: ['src/http.ts', 'src/httpApi.ts', 'src/sqs.ts', 'src/sns.ts', 'src/migration-run.ts', 'src/dns-eventbridge.ts'], target: 'node18', clean: true, minify: config.NODE_ENV === 'production',