Skip to content

Commit

Permalink
api gateway event 덤프 뜨는 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
if1live committed Oct 3, 2023
1 parent b05c7c5 commit 915ea31
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 108 deletions.
17 changes: 16 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HTTPException } from "hono/http-exception";
import { engine, dynamodb } from "./instances.js";
import { touch } from "./services.js";
import { deleteResult, loadSortedResults } from "./stores.js";
import { getAPIGatewayInput } from "./helpers.js";

const robotsTxt = `
User-agent: *
Expand Down Expand Up @@ -111,6 +112,7 @@ app.get("/sentry/message", async (c) => {
app.get("/sentry/error/handled", async (c) => {
try {
const e = new Error("handled error");
e.name = "HandledError";
(e as any).foo = "bar";
throw e;
} catch (e: unknown) {
Expand All @@ -120,7 +122,10 @@ app.get("/sentry/error/handled", async (c) => {
});

app.get("/sentry/error/unhandled", async (c) => {
throw new Error("unhandled error");
const e = new Error("unhandled error");
e.name = "UnhandledError";
(e as any).a = 1;
throw e;
});

app.get("/error/plain", async (c) => {
Expand All @@ -134,6 +139,16 @@ app.get("/error/http", async (c) => {
throw new HTTPException(403, { message: "hono-http-exception" });
});

app.all("/dump", async (c) => {
const apiGateway = getAPIGatewayInput();
if (!apiGateway) {
return c.json({ message: "apigateway not found" });
}

const { event, context } = apiGateway;
return c.json(event);
});

app.get("*", async (c) => {
throw new HTTPException(404, { message: `not found` });
});
6 changes: 3 additions & 3 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { APIGatewayProxyHandlerV2, ScheduledHandler } from "aws-lambda";
import * as Sentry from "@sentry/serverless";
import { app } from "./app.js";
import { handle } from "hono/aws-lambda";
import { touch } from "./services.js";
import { SENTRY_DSN, NODE_ENV } from "./settings.js";
import { wrap_apigateway } from "./helpers.js";

if (SENTRY_DSN) {
Sentry.AWSLambda.init({
Expand All @@ -18,9 +18,9 @@ if (SENTRY_DSN) {
});
}

const http_inner = handle(app);
const http_inner = wrap_apigateway(app);
export const http: APIGatewayProxyHandlerV2 = async (event, context) => {
const response = await http_inner(event as any);
const response = await http_inner(event, context);
return response;
};

Expand Down
40 changes: 40 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AsyncLocalStorage } from "node:async_hooks";
import {
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
APIGatewayProxyStructuredResultV2,
Context,
} from "aws-lambda";
import { Hono } from "hono";
import { handle } from "hono/aws-lambda";

export type APIGatewayInput = {
event: APIGatewayProxyEventV2 | APIGatewayProxyEvent;
context: Context;
};

export const asyncLocalStorage = new AsyncLocalStorage<APIGatewayInput>();

export function getAPIGatewayInput(): APIGatewayInput | undefined {
return asyncLocalStorage.getStore();
}

export const wrap_apigateway = (app: Hono) => {
const handle_core = handle(app);

return async (
event: APIGatewayProxyEventV2 | APIGatewayProxyEvent,
context: Context,
): Promise<APIGatewayProxyStructuredResultV2> => {
const apigw: APIGatewayInput = {
event,
context,
};

const response = await asyncLocalStorage.run(apigw, async () => {
return await handle_core(event as any);
});

return response;
};
};
104 changes: 0 additions & 104 deletions src/router.ts

This file was deleted.

0 comments on commit 915ea31

Please sign in to comment.