From ae527b9d866520696f5c03b02c00651c7b6617fc Mon Sep 17 00:00:00 2001 From: Aponia Date: Mon, 16 Oct 2023 16:46:21 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20warming=20body=20in=20lambd?= =?UTF-8?q?a=20lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/bronya.config.ts | 9 +-------- libs/lambda/src/handler.ts | 30 ++++++++++++++++++++++++++++++ libs/lambda/src/index.ts | 2 ++ libs/lambda/src/request.ts | 7 +++++++ 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 libs/lambda/src/handler.ts create mode 100644 libs/lambda/src/request.ts diff --git a/apps/api/bronya.config.ts b/apps/api/bronya.config.ts index d53e8d87..9c082989 100644 --- a/apps/api/bronya.config.ts +++ b/apps/api/bronya.config.ts @@ -4,7 +4,7 @@ import { join, resolve } from "node:path"; import { Api, type ApiConstructProps } from "@bronya.js/api-construct"; import { createApiCliPlugins } from "@bronya.js/api-construct/plugins/cli"; import { isCdk } from "@bronya.js/core"; -import { logger } from "@libs/lambda"; +import { logger, warmingRequestBody } from "@libs/lambda"; import { LambdaIntegration, ResponseType } from "aws-cdk-lib/aws-apigateway"; import { Certificate } from "aws-cdk-lib/aws-certificatemanager"; import { RuleTargetInput, Rule, Schedule } from "aws-cdk-lib/aws-events"; @@ -79,13 +79,6 @@ const prismaSchema = resolve(libsDbDirectory, "prisma", prismaSchemaFile); */ const prismaQueryEngineFile = "libquery_engine-linux-arm64-openssl-1.0.x.so.node"; -/** - * The body of a warming request. - * - * TODO: actually recognize warming requests in the route handlers. - */ -const warmingRequestBody = { body: "warming request" }; - /** * Shared ESBuild options. */ diff --git a/libs/lambda/src/handler.ts b/libs/lambda/src/handler.ts new file mode 100644 index 00000000..b882008a --- /dev/null +++ b/libs/lambda/src/handler.ts @@ -0,0 +1,30 @@ +import type { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"; + +import { warmingRequestBody } from "./request"; +import { createOKResult, createErrorResult } from "./response"; + +export type ResponseHelpers = { + ok: typeof createOKResult; + error: typeof createErrorResult; +}; + +export type ExtendedApiGatewayHandler = ( + event: APIGatewayProxyEvent, + context: Context, + res: ResponseHelpers, +) => APIGatewayProxyResult | Promise; + +export function createHandler(handler: ExtendedApiGatewayHandler): ExtendedApiGatewayHandler { + return async function (event, context) { + const res: ResponseHelpers = { + ok: (payload, headers, requestId) => createOKResult(payload, headers, requestId), + error: (statusCode, e, requestId) => createErrorResult(statusCode, e, requestId), + }; + + if (event.body === JSON.stringify(warmingRequestBody)) { + return res.ok("Successfully warmed!", event.headers, context.awsRequestId); + } + + return handler(event, context, res); + }; +} diff --git a/libs/lambda/src/index.ts b/libs/lambda/src/index.ts index 4747115a..13471763 100644 --- a/libs/lambda/src/index.ts +++ b/libs/lambda/src/index.ts @@ -2,3 +2,5 @@ export * from "./logger"; export * from "./compress"; export * from "./response"; export * from "./constants"; +export * from "./request"; +export * from "./handler"; diff --git a/libs/lambda/src/request.ts b/libs/lambda/src/request.ts new file mode 100644 index 00000000..a824b231 --- /dev/null +++ b/libs/lambda/src/request.ts @@ -0,0 +1,7 @@ +/** + * The body of a warming request. + * + * A warming request is periodically sent to ensure that the Lambda function is active. + * Ideally, it wouldn't trigger any expensive computations. + */ +export const warmingRequestBody = { body: "warming request" };