Skip to content

Commit

Permalink
Add webhook refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedal987 committed Jun 15, 2024
1 parent d5ac584 commit 83573bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions ebs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "esbuild --bundle --minify --platform=node --sourcemap=inline --outfile=dist/index.js src/index.ts"
},
"dependencies": {
"@octokit/webhooks": "^13.2.7",
"@twurple/api": "^7.1.0",
"@twurple/ebs-helper": "^7.1.0",
"body-parser": "^1.20.2",
Expand Down
21 changes: 21 additions & 0 deletions ebs/src/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { sendPubSubMessage } from "../util/pubsub";
import { strToU8, compressSync, strFromU8 } from "fflate";
import { getBannedUsers } from "../util/db";
import { asyncCatch } from "../util/middleware";
import { Webhooks } from "@octokit/webhooks";

let activeConfig: Config | undefined;
let configData: Config | undefined;
Expand Down Expand Up @@ -84,6 +85,26 @@ app.get("/private/refresh", asyncCatch(async (_, res) => {
res.sendStatus(200);
}));

const webhooks = new Webhooks({
secret: process.env.PRIVATE_API_KEY!,
});

app.post("/webhook/refresh", asyncCatch(async (req, res) => {
// github webhook
const signature = req.headers["x-hub-signature-256"] as string;
const body = req.body as string;

if(!(await webhooks.verify(body, signature))) {
res.sendStatus(403);
return;
}

await refreshConfig();
console.log("Refreshed config, new config version is ", activeConfig!.version);
await broadcastConfigRefresh(activeConfig!);
res.sendStatus(200);
}));

app.get("/public/config", asyncCatch(async (req, res) => {
const config = await getConfig();
res.send(JSON.stringify(config));
Expand Down

0 comments on commit 83573bb

Please sign in to comment.