From 9b22ac70b7e318ad2db03aaea5265c390ef0ad75 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Sun, 4 Sep 2022 14:54:10 +0200 Subject: [PATCH] feat: add redirect to root --- .env.example | 1 + lib/config.ts | 1 + lib/handler.ts | 10 +++++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 081ca5c..fb9fde2 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ SIGN_KEY= DEBUG=0 +MAIN_REDIRECT=https://github.com/shiftinv/github-webhook-filter diff --git a/lib/config.ts b/lib/config.ts index ae1399d..ba3503b 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -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), }; diff --git a/lib/handler.ts b/lib/handler.ts index b60fc6a..92a1bde 100644 --- a/lib/handler.ts +++ b/lib/handler.ts @@ -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 { + 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);