Skip to content

Commit

Permalink
Log for testWebhookReceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
tkiapril committed Aug 3, 2023
1 parent d556ee7 commit 3da8de2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
EmitterLoggerName,
getInternalLoggers,
ObserverLoggerName,
TestWebhookReceiverLoggerName,
WebLoggerName,
} from "./logUtils.ts";
import { observer } from "./observer.ts";
Expand Down Expand Up @@ -101,6 +102,10 @@ async function main() {
level: "DEBUG",
handlers: ["console"],
},
[TestWebhookReceiverLoggerName]: {
level: "INFO",
handlers: ["console"],
},
lop: {
level: "INFO",
handlers: ["console"],
Expand Down
1 change: 1 addition & 0 deletions logUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const WebLoggerName = "web";
// dev utility logger names
export const DevLoggerName = "dev";
export const DataproxyLoggerName = "dataproxy";
export const TestWebhookReceiverLoggerName = "testWebhookReceiver";

// internal logger names
export const ControlLoggerName = "control";
Expand Down
38 changes: 35 additions & 3 deletions testWebhookReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { ConsoleHandler } from "https://deno.land/[email protected]/log/handlers.ts";
import {
getLogger,
setup as setupLog,
} from "https://deno.land/[email protected]/log/mod.ts";

import {
Application as OakApplication,
} from "https://deno.land/x/[email protected]/mod.ts";

import { parse } from "npm:lossless-json";

import {
defaultLogFormatter,
TestWebhookReceiverLoggerName,
} from "./logUtils.ts";
import { runAndCleanup } from "./runHelpers.ts";

function numberParser(value: string) {
Expand All @@ -11,10 +23,11 @@ function numberParser(value: string) {
}

export async function testWebhookReceiver() {
const logger = getLogger(TestWebhookReceiverLoggerName);
const abortController = new AbortController();
const app = new OakApplication();
app.use(async (ctx) => {
console.log(
logger.info(
"Received Webhook:",
parse(
await ctx.request.body({ type: "text" }).value,
Expand All @@ -25,17 +38,36 @@ export async function testWebhookReceiver() {
ctx.response.body = "";
});

const port = 8888;
logger.info(`Test webhook receiver listening on port ${port}.`);
const runningPromise = app.listen({
port: 8888,
port,
signal: abortController.signal,
});

async function cleanup() {
logger.info(`Stopping test webhook receiver.`);
abortController.abort();
await runningPromise;
}

return await Promise.resolve({ runningPromise, cleanup });
}

if (import.meta.main) await runAndCleanup(testWebhookReceiver);
if (import.meta.main) {
setupLog({
handlers: {
console: new ConsoleHandler("DEBUG", {
formatter: defaultLogFormatter,
}),
},

loggers: {
[TestWebhookReceiverLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
},
});
await runAndCleanup(testWebhookReceiver);
}

0 comments on commit 3da8de2

Please sign in to comment.