diff --git a/.gitignore b/.gitignore index 2215944..f0a223e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ transpiled/ worker/ .hugo_build.lock yarn.lock + +# Local Netlify folder +.netlify diff --git a/netlify.toml b/netlify.toml index c6d9b15..21c34b3 100644 --- a/netlify.toml +++ b/netlify.toml @@ -4,3 +4,7 @@ [build.environment] HUGO_VERSION = "0.104.3" + +[[edge_functions]] +path = "/*" +function = "root" diff --git a/netlify/edge-functions/root.ts b/netlify/edge-functions/root.ts new file mode 100644 index 0000000..1087be3 --- /dev/null +++ b/netlify/edge-functions/root.ts @@ -0,0 +1,56 @@ +import type { Context } from "https://edge.netlify.com"; + +const GITHUB_USERNAME = "msfjarvis"; +const APS_SLUG = "Android-Password-Store"; +const GITHUB_URL = `https://github.com`; +const MY_GITHUB = `${GITHUB_URL}/${GITHUB_USERNAME}`; +const APS_GITHUB_URL = `https://github.com/${APS_SLUG}/${APS_SLUG}`; + +export default async (request: Request, context: Context) => { + const url = new URL(request.url); + const urlParts = url.pathname.split("/").filter(entry => entry != ""); + context.log(urlParts); + switch (urlParts[0]) { + case "g": + switch (urlParts.length) { + case 1: + return redirect(MY_GITHUB); + case 2: + return redirect(`${MY_GITHUB}/${urlParts[1]}`); + case 3: + return redirect(`${MY_GITHUB}/${urlParts[1]}/commit/${urlParts[2]}`); + case 4: + return redirect(`${MY_GITHUB}/${urlParts[1]}/issues/${urlParts[3]}`); + } + case "aps": + switch (urlParts.length) { + case 1: + return redirect(APS_GITHUB_URL); + case 2: + return redirect(`${APS_GITHUB_URL}/commit/${urlParts[1]}`); + case 3: + return redirect(`${APS_GITHUB_URL}/issues/${urlParts[2]}`); + } + case "apsg": + switch (urlParts.length) { + case 1: + return redirect(`${GITHUB_URL}/${APS_SLUG}`); + case 2: + return redirect(`${GITHUB_URL}/${APS_SLUG}/${urlParts[1]}`); + case 3: + return redirect( + `${GITHUB_URL}/${APS_SLUG}/${urlParts[1]}/commit/${urlParts[2]}` + ); + case 4: + return redirect( + `${GITHUB_URL}/${APS_SLUG}/${urlParts[1]}/issues/${urlParts[3]}` + ); + } + default: + return context.next(); + } +}; + +async function redirect(url: string): Promise { + return Response.redirect(url, 301); +}