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";
|
2022-09-06 01:56:57 +00:00
|
|
|
import { requestLog } from "./lib/util.ts";
|
2022-09-02 20:24:33 +00:00
|
|
|
|
2022-09-04 12:34:35 +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
|
|
|
},
|
2022-09-04 12:34:35 +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> {
|
2022-09-06 01:56:57 +00:00
|
|
|
const reqLog = requestLog(req.headers);
|
2022-09-06 02:16:06 +00:00
|
|
|
|
2022-09-06 02:00:52 +00:00
|
|
|
let res: Response;
|
2022-09-06 02:16:06 +00:00
|
|
|
let meta: Record<string, string> | null = null;
|
2022-09-02 20:24:33 +00:00
|
|
|
try {
|
2022-09-06 02:16:06 +00:00
|
|
|
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) {
|
2022-09-06 01:56:57 +00:00
|
|
|
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 {
|
2022-09-06 01:56:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:16:06 +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;
|
2022-09-06 01:56:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-04 12:34:35 +00:00
|
|
|
if (import.meta.main) {
|
2022-09-06 00:59:05 +00:00
|
|
|
await setupLogs();
|
2022-09-04 12:34:35 +00:00
|
|
|
|
2022-09-04 12:39:03 +00:00
|
|
|
if (!config.signKey) {
|
|
|
|
log.warning("url signing disabled");
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:34:35 +00:00
|
|
|
log.info(`starting webserver on ${config.hostname}:${config.port}`);
|
|
|
|
http.serve(handleRequest, {
|
|
|
|
hostname: config.hostname,
|
|
|
|
port: config.port,
|
2022-09-06 01:02:28 +00:00
|
|
|
onListen: () => log.info(`listening on ${config.hostname}:${config.port}`),
|
2022-09-04 12:34:35 +00:00
|
|
|
});
|
|
|
|
}
|