2024-02-01 16:30:36 +00:00
|
|
|
import { httpErrors, log } from "./deps.ts";
|
2022-09-02 20:24:33 +00:00
|
|
|
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
|
|
|
|
2024-02-01 16:14:59 +00:00
|
|
|
function setupLogs() {
|
|
|
|
log.setup({
|
2022-09-04 12:34:35 +00:00
|
|
|
handlers: {
|
2024-02-01 16:14:59 +00:00
|
|
|
console: new log.ConsoleHandler("DEBUG", {
|
2022-09-04 12:34:35 +00:00
|
|
|
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
|
|
|
|
2024-02-01 16:30:36 +00:00
|
|
|
async function handleRequest(req: Request, info: Deno.ServeHandlerInfo): 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) {
|
2024-02-01 16:14:59 +00:00
|
|
|
if (err instanceof httpErrors.HttpError && err.expose) {
|
2024-02-01 16:30:36 +00:00
|
|
|
reqLog.warn(`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);
|
|
|
|
}
|
|
|
|
|
2022-09-10 01:03:21 +00:00
|
|
|
// remove other headers that don't make sense here
|
|
|
|
for (const header of ["set-cookie", "alt-svc"]) {
|
|
|
|
res.headers.delete(header);
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:16:06 +00:00
|
|
|
// log request
|
2022-09-06 02:00:52 +00:00
|
|
|
const respLen = res.headers.get("content-length") || 0;
|
2024-02-01 16:30:36 +00:00
|
|
|
const addr = info.remoteAddr;
|
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) {
|
2024-02-01 16:14:59 +00:00
|
|
|
setupLogs();
|
2022-09-04 12:34:35 +00:00
|
|
|
|
2024-02-01 16:32:59 +00:00
|
|
|
if (config.signKey) {
|
|
|
|
log.info("url signing enabled");
|
2022-09-04 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 12:34:35 +00:00
|
|
|
log.info(`starting webserver on ${config.hostname}:${config.port}`);
|
2024-02-01 16:30:36 +00:00
|
|
|
Deno.serve({
|
2022-09-04 12:34:35 +00:00
|
|
|
hostname: config.hostname,
|
|
|
|
port: config.port,
|
2022-09-06 01:02:28 +00:00
|
|
|
onListen: () => log.info(`listening on ${config.hostname}:${config.port}`),
|
2024-02-01 16:30:36 +00:00
|
|
|
}, handleRequest);
|
2022-09-04 12:34:35 +00:00
|
|
|
}
|