From 18b19ebdfb7c7154b629d3d75c194a008a381a16 Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Halili Date: Fri, 19 Jul 2024 00:21:11 +0800 Subject: [PATCH] feat(golinks): bring over some redirects from old KV backend Alongside improve API docs Signed-off-by: Andrei Jiroh Halili --- apps/golinks-v2/src/{index.ts => index.tsx} | 35 +++++++++++++++++-- apps/golinks-v2/src/lib/constants.ts | 25 +++++++++++-- apps/golinks-v2/src/pages/deprecated-link.tsx | 35 +++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) rename apps/golinks-v2/src/{index.ts => index.tsx} (74%) create mode 100644 apps/golinks-v2/src/pages/deprecated-link.tsx diff --git a/apps/golinks-v2/src/index.ts b/apps/golinks-v2/src/index.tsx similarity index 74% rename from apps/golinks-v2/src/index.ts rename to apps/golinks-v2/src/index.tsx index 5171f84..6cc0ef4 100644 --- a/apps/golinks-v2/src/index.ts +++ b/apps/golinks-v2/src/index.tsx @@ -4,9 +4,10 @@ import { Context, Hono } from "hono"; import { cors } from "hono/cors"; import { EnvBindings, Env } from "./types"; import { getDiscordInvite, getLink } from "lib/db"; -import { adminApiKey, contact, getWorkersDashboardUrl, homepage, servers, errorMessages } from "lib/constants"; +import { adminApiKey, contact, getWorkersDashboardUrl, homepage, servers, errorMessages, tags } from "lib/constants"; import { DiscordInviteLinkCreate, DiscordInviteLinkList } from "api/discord"; import { adminApiKeyAuth } from "lib/auth"; +import { DeprecatedGoLinkPage } from "pages/deprecated-link" // Start a Hono app const app = new Hono<{ Bindings: EnvBindings }>(); @@ -40,10 +41,15 @@ const openapi = fromHono(app, { }, }, servers, + tags, + externalDocs: { + description: "Learn more about golinks and this service", + url: homepage + } }, }); -openapi.registry.registerComponent("securitySchema", "adminApiKey", adminApiKey); +openapi.registry.registerComponent("securitySchemes", "adminApiKey", adminApiKey); // Register OpenAPI endpoints openapi.get("/api/links", GoLinkList); @@ -62,10 +68,13 @@ app.get("/favicon.ico", (c) => { app.get("/workers/edit", (c) => { return c.redirect("https://github.dev/andreijiroh-dev/api-servers/blob/main/apps/golinks-v2/src/index.ts"); }); - app.get("/workers/dashboard", (c) => { return c.redirect(getWorkersDashboardUrl(c.env.DEPLOY_ENV)); }); +app.get("/workers", (c) => { + const {origin} = new URL(c.req.url) + return c.redirect(`${origin}/workers/dashboard`) +}) app.get("/:link", async (c) => { try { @@ -103,5 +112,25 @@ app.get("/discord/:inviteCode", async (c) => { } }); +/* Old /edit/* stuff */ +app.get("/edit", (c) => { + return c.redirect("/workers/edit") +}) +app.get("/edit/links", (c) => { + if (!c.req.query("force_redirect")) { + return c.redirect("/landing/deprecated?golink=edit/links&reason=golinks+KV+backend+no+longer+in+use,+now+points+to+feedback+link", 301) + } + return c.redirect("/feedback/suggest-new-golink") +}) +app.get("/landing/deprecated", (c) => { + const params = c.req.query() + + if (!c.req.param()) { + return c.newResponse("This is unexpected request for this route", 400) + } + + return c.html() +}) + // Export the Hono app export default app; diff --git a/apps/golinks-v2/src/lib/constants.ts b/apps/golinks-v2/src/lib/constants.ts index 9aaabf2..b6f338a 100644 --- a/apps/golinks-v2/src/lib/constants.ts +++ b/apps/golinks-v2/src/lib/constants.ts @@ -1,5 +1,4 @@ -import { EnvBindings } from "types"; -import { Env } from "../../worker-configuration"; +import { EnvBindings, Env } from "types"; export const adminApiKey = { type: "apiKey", @@ -8,10 +7,11 @@ export const adminApiKey = { }; export const homepage = "https://wiki.andreijiroh.xyz/golinks"; +export const sources = "https://github.com/andreijiroh-dev/api-servers/tree/main/apps/golinks-v2"; export const contact = { name: "Andrei Jiroh Halili", - url: homepage, + url: sources, email: "ajhalili2006@andreijiroh.xyz", }; @@ -42,6 +42,25 @@ export const servers = [ }, ]; +export const tags = [ + { + name: "golinks", + description: "Personal golinks and link shortener", + externalDocs: { + description: "Learn more about golinks", + url: homepage + } + }, + { + name: "discord-invites", + description: "Custom Discord invite links at `go/discord/`", + externalDocs: { + description: "Add your Discord invite code", + url: "https://go.andreijiroh.xyz/feedback/add-discord-invite" + } + } +] + export const errorMessages = { discordServerNotFound: `\ Either that server is not on our records or something went wrong on our side. diff --git a/apps/golinks-v2/src/pages/deprecated-link.tsx b/apps/golinks-v2/src/pages/deprecated-link.tsx new file mode 100644 index 0000000..8e89b88 --- /dev/null +++ b/apps/golinks-v2/src/pages/deprecated-link.tsx @@ -0,0 +1,35 @@ +import type { FC } from "hono/jsx" + +const Layout: FC = (props,) => { + return ( + + + Deprecated golink landing page + + {props.children} + + ) +} + +export const DeprecatedGoLinkPage: FC<{ + url: string, + golink: string, + reason: string | "We're changing things up" +}> = (props: { + golink: string, + reason: string | null +})=> { + const forceRedirect = `/${props.golink}?force_redirect=1` + return( + +

Deprecated golink: go/{props.golink}

+ This golink is being flagged as deprecated due to the following reason: +
    +
  • {props.reason || "No reason provided yet" }
  • +
+

If the golink is being renamed as the reason, please update your URLs. If you still want to go there, please click here.

+
+

The lack of CSS is intentional to keep things simple. It will be added soon.

+
+ ) +} -- 2.51.2