github-webhook-filter/main.ts

80 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-09-02 20:24:33 +00:00
import { http, log } from "./deps.ts";
import config from "./lib/config.ts";
import handler from "./lib/handler.ts";
import { requestLog } from "./lib/util.ts";
2022-09-02 20:24:33 +00:00
async function setupLogs(): Promise<void> {
await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG", {
formatter: (rec) => `${rec.datetime.toISOString()} [${rec.levelName}] ${rec.msg}`,
}),
2022-09-02 20:24:33 +00:00
},
loggers: {
default: {
level: config.debug ? "DEBUG" : "INFO",
handlers: ["console"],
},
},
});
}
2022-09-02 20:24:33 +00:00
async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Response> {
const reqLog = requestLog(req.headers);
2022-09-06 02:00:52 +00:00
let res: Response;
let meta: Record<string, string> | null = null;
2022-09-02 20:24:33 +00:00
try {
const webhookResult = await handler(req);
if (webhookResult instanceof Response) {
res = webhookResult;
} else {
[res, meta] = webhookResult;
}
2022-09-02 20:24:33 +00:00
} catch (err) {
if (http.isHttpError(err) && err.expose) {
reqLog.warning(`http error: ${err.message}`);
2022-09-06 02:00:52 +00:00
res = new Response(err.message, { status: err.status });
2022-09-02 20:24:33 +00:00
} else {
reqLog.critical(err);
2022-09-06 02:00:52 +00:00
res = new Response("Internal Server Error", { status: 500 });
2022-09-02 20:24:33 +00:00
}
}
// clone response to make headers mutable
res = new Response(res.body, res);
// set metadata headers
meta = {
"deploy": config.deployId,
...meta,
};
for (const [key, value] of Object.entries(meta)) {
res.headers.set(`x-webhook-filter-${key}`, value);
}
// log request
2022-09-06 02:00:52 +00:00
const respLen = res.headers.get("content-length") || 0;
2022-09-02 20:24:33 +00:00
const addr = connInfo.remoteAddr as Deno.NetAddr;
reqLog.info(
2022-09-06 02:00:52 +00:00
`http: ${addr.hostname}:${addr.port} - ${req.method} ${req.url} ${res.status} ${respLen}`,
2022-09-02 20:24:33 +00:00
);
2022-09-06 02:00:52 +00:00
return res;
2022-09-02 20:24:33 +00:00
}
if (import.meta.main) {
2022-09-06 00:59:05 +00:00
await setupLogs();
2022-09-04 12:39:03 +00:00
if (!config.signKey) {
log.warning("url signing disabled");
}
log.info(`starting webserver on ${config.hostname}:${config.port}`);
http.serve(handleRequest, {
hostname: config.hostname,
port: config.port,
onListen: () => log.info(`listening on ${config.hostname}:${config.port}`),
});
}