feat: set metadata headers on all responses
This commit is contained in:
parent
b89fd630f1
commit
188aadf638
|
@ -6,7 +6,9 @@ import { UrlConfig } from "./types.d.ts";
|
|||
import { parseBool, requestLog } from "./util.ts";
|
||||
import { sendWebhook } from "./webhook.ts";
|
||||
|
||||
export default async function handle(req: Request): Promise<Response> {
|
||||
export default async function handle(
|
||||
req: Request,
|
||||
): Promise<Response | [Response, Record<string, string>]> {
|
||||
const url = new URL(req.url);
|
||||
|
||||
// redirect to repo if `GET /`
|
||||
|
|
|
@ -6,7 +6,7 @@ export async function sendWebhook(
|
|||
token: string,
|
||||
headers: Headers,
|
||||
body: string,
|
||||
): Promise<Response> {
|
||||
): Promise<[Response, Record<string, string>]> {
|
||||
const reqLog = requestLog(headers);
|
||||
const url = `https://discord.com/api/webhooks/${id}/${token}/github?wait=1`;
|
||||
|
||||
|
@ -54,22 +54,9 @@ export async function sendWebhook(
|
|||
await sleep(resetms);
|
||||
} while (true);
|
||||
|
||||
// clone response to make headers mutable
|
||||
res = new Response(res.body, res);
|
||||
|
||||
// set metadata headers
|
||||
const meta: Record<string, string> = {
|
||||
"deploy": config.deployId,
|
||||
};
|
||||
// set metadata
|
||||
const meta: Record<string, string> = {};
|
||||
if (retries) meta["retries"] = retries.toString();
|
||||
|
||||
setMetadata(res, meta);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function setMetadata(res: Response, meta: Record<string, string>): void {
|
||||
for (const [key, value] of Object.entries(meta)) {
|
||||
res.headers.set(`x-webhook-filter-${key}`, value);
|
||||
}
|
||||
return [res, meta];
|
||||
}
|
||||
|
|
22
main.ts
22
main.ts
|
@ -21,9 +21,16 @@ async function setupLogs(): Promise<void> {
|
|||
|
||||
async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Response> {
|
||||
const reqLog = requestLog(req.headers);
|
||||
|
||||
let res: Response;
|
||||
let meta: Record<string, string> | null = null;
|
||||
try {
|
||||
res = await handler(req);
|
||||
const webhookResult = await handler(req);
|
||||
if (webhookResult instanceof Response) {
|
||||
res = webhookResult;
|
||||
} else {
|
||||
[res, meta] = webhookResult;
|
||||
}
|
||||
} catch (err) {
|
||||
if (http.isHttpError(err) && err.expose) {
|
||||
reqLog.warning(`http error: ${err.message}`);
|
||||
|
@ -34,6 +41,19 @@ async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Res
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
const respLen = res.headers.get("content-length") || 0;
|
||||
const addr = connInfo.remoteAddr as Deno.NetAddr;
|
||||
reqLog.info(
|
||||
|
|
Loading…
Reference in New Issue