github-webhook-filter/main.ts

47 lines
1.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";
await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG", {
formatter: (rec) => `${rec.datetime.toISOString()} [${rec.levelName}] ${rec.msg}`,
}),
},
loggers: {
default: {
level: config.debug ? "DEBUG" : "INFO",
handlers: ["console"],
},
},
});
async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Response> {
let resp: Response;
try {
resp = await handler(req);
} catch (err) {
if (http.isHttpError(err) && err.expose) {
log.warning(`http error: ${err.message}`);
resp = new Response(err.message, { status: err.status });
} else {
log.critical(err);
resp = new Response("Internal Server Error", { status: 500 });
}
}
const respLen = resp.headers.get("content-length") || 0;
const addr = connInfo.remoteAddr as Deno.NetAddr;
log.info(
`http: ${addr.hostname}:${addr.port} - ${req.method} ${req.url} ${resp.status} ${respLen}`,
);
return resp;
}
log.info(`Starting webserver on ${config.hostname}:${config.port}`);
http.serve(handleRequest, {
hostname: config.hostname,
port: config.port,
});