Skip to content

Commit

Permalink
feat: set x-webhook-filter-deploy on all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Dec 11, 2024
1 parent d88eded commit 6dab963
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import config from "./config.ts";
import { contextMiddleware, getRequestLog } from "./context.ts";
import { hasKey, verify } from "./crypto.ts";
import handler from "./handler.ts";
import { setMetaHeader } from "./util.ts";

const app = new Hono();

Expand All @@ -14,6 +15,12 @@ app.use(contextMiddleware);
// use context's requestlog for server logs
app.use(logger((...args) => getRequestLog().log(...args)));

// add deploy header to responses
app.use(async (c, next) => {
await next();
setMetaHeader(c, "deploy", config.deployId);
});

if (config.mainRedirect) {
app.get("/", (c) => {
return c.redirect(config.mainRedirect!);
Expand All @@ -38,12 +45,8 @@ app.post("/:id/:token", async (c) => {
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);
setMetaHeader(c, key, value);
}

// remove other headers that don't make sense here
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Context } from "@hono/hono";

export function parseBool(s: string): boolean {
return ["1", "true", "on", "y", "yes"].includes(s.toLowerCase());
}
Expand Down Expand Up @@ -51,3 +53,7 @@ export function requestLog(deliveryId: string | undefined) {
}

export type RequestLog = ReturnType<typeof requestLog>;

export function setMetaHeader(c: Context, key: string, value: string): void {
c.res.headers.set(`x-webhook-filter-${key}`, value);
}

0 comments on commit 6dab963

Please sign in to comment.