feat: add redirect to root

This commit is contained in:
shiftinv 2022-09-04 14:54:10 +02:00
parent ff98d3c9a0
commit 9b22ac70b7
3 changed files with 11 additions and 1 deletions

View File

@ -1,2 +1,3 @@
SIGN_KEY=
DEBUG=0
MAIN_REDIRECT=https://github.com/shiftinv/github-webhook-filter

View File

@ -16,4 +16,5 @@ export default {
signKey: get("SIGN_KEY", null),
maxWebhookRetries: parseInt(get("MAX_RETRIES", "3")),
maxWebhookRetryMs: parseInt(get("MAX_RETRY_MS", "30000")),
mainRedirect: get("MAIN_REDIRECT", null),
};

View File

@ -1,4 +1,5 @@
import { http, log } from "../deps.ts";
import config from "./config.ts";
import { hasKey, verify } from "./crypto.ts";
import filterWebhook from "./filter.ts";
import { UrlConfig } from "./types.d.ts";
@ -6,12 +7,19 @@ import { parseBool } from "./util.ts";
import { sendWebhook } from "./webhook.ts";
export default async function handle(req: Request): Promise<Response> {
const url = new URL(req.url);
// redirect to repo if `GET /`
if (req.method === "GET" && config.mainRedirect && url.pathname === "/") {
return Response.redirect(config.mainRedirect);
}
// everything else should be a POST
if (req.method !== "POST") {
throw http.createHttpError(405);
}
// split url into parts
const url = new URL(req.url);
const [, id, token] = url.pathname.split("/");
if (!id || !token) {
throw http.createHttpError(400);