Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: ✨ warming body in lambda lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0nia committed Oct 16, 2023
1 parent 62853c3 commit ae527b9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
9 changes: 1 addition & 8 deletions apps/api/bronya.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
*/
Expand Down
30 changes: 30 additions & 0 deletions libs/lambda/src/handler.ts
Original file line number Diff line number Diff line change
@@ -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<APIGatewayProxyResult>;

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);
};
}
2 changes: 2 additions & 0 deletions libs/lambda/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from "./logger";
export * from "./compress";
export * from "./response";
export * from "./constants";
export * from "./request";
export * from "./handler";
7 changes: 7 additions & 0 deletions libs/lambda/src/request.ts
Original file line number Diff line number Diff line change
@@ -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" };

0 comments on commit ae527b9

Please sign in to comment.