Skip to content

refactor: split http api function and prevent isolate calls #11544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/deployment-service/cdk/lib/cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 0 additions & 2 deletions packages/deployment-service/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ async function bootstrapServer(): Promise<Server> {
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
}
60 changes: 60 additions & 0 deletions packages/deployment-service/src/httpApi.ts
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort of? Will need to refactor the auth method tbh


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<Server> {
if (!cachedServer) {
const [app, express] = await bootstrapApplication()

app.use(eventContext())

await app.init()

cachedServer = createServer(express, undefined, binaryMimeTypes)
}

return cachedServer
}

export const handler: Handler<APIGatewayEvent, APIGatewayProxyResult> = 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
}
2 changes: 1 addition & 1 deletion packages/deployment-service/tsup-zip.config.json
Original file line number Diff line number Diff line change
@@ -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"]
}
2 changes: 1 addition & 1 deletion packages/deployment-service/tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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'],

Check notice on line 10 in packages/deployment-service/tsup.config.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/deployment-service/tsup.config.js#L10

Replace `'src/http.ts',·'src/httpApi.ts',·'src/sqs.ts',·'src/sns.ts',·'src/migration-run.ts',·'src/dns-eventbridge.ts'` with `⏎······'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',
Expand Down
Loading