From 2cd96f763ef869fc7da1e64635181a75c7b19e3c Mon Sep 17 00:00:00 2001 From: oiov Date: Wed, 31 Jul 2024 17:31:43 +0800 Subject: [PATCH] feat: add meta geo --- app/(protected)/admin/loading.tsx | 3 +-- app/api/s/route.ts | 18 +++++++++++------- config/docs.ts | 20 ++++++++++++++++++++ content/docs/exanples/other.mdx | 5 +++++ content/docs/exanples/vercel.mdx | 5 +++++ content/docs/exanples/zeabur.mdx | 4 ++++ middleware.ts | 29 ++++++++++++++++++++++------- 7 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 content/docs/exanples/other.mdx create mode 100644 content/docs/exanples/vercel.mdx create mode 100644 content/docs/exanples/zeabur.mdx diff --git a/app/(protected)/admin/loading.tsx b/app/(protected)/admin/loading.tsx index 261bf55..296546a 100644 --- a/app/(protected)/admin/loading.tsx +++ b/app/(protected)/admin/loading.tsx @@ -9,8 +9,7 @@ export default function AdminPanelLoading() { text="Access only for users with ADMIN role." />
-
- +
diff --git a/app/api/s/route.ts b/app/api/s/route.ts index d6fb918..4314292 100644 --- a/app/api/s/route.ts +++ b/app/api/s/route.ts @@ -7,21 +7,25 @@ export async function GET(req: NextRequest) { const url = new URL(req.url); const slug = url.searchParams.get("slug"); const ip = url.searchParams.get("ip"); + const city = url.searchParams.get("city"); + const region = url.searchParams.get("region"); + const country = url.searchParams.get("country"); + const latitude = url.searchParams.get("latitude"); + const longitude = url.searchParams.get("longitude"); if (!slug || !ip) return Response.json(null); const res = await getUrlBySuffix(slug); if (res?.target && res?.active === 1) { - console.log("[api/s]", ip, res.id); - // TODO + // console.log("[api/s]", ip, res.id); await createUserShortUrlMeta({ urlId: res.id, click: 1, ip: ip ? ip.split(",")[0] : "127.0.0.1", - city: "", - region: "", - country: "", - latitude: "", - longitude: "", + city, + region, + country, + latitude, + longitude, }); return Response.json(res.target); } diff --git a/config/docs.ts b/config/docs.ts index a4f16ec..3f21d74 100644 --- a/config/docs.ts +++ b/config/docs.ts @@ -43,6 +43,26 @@ export const docsConfig: DocsConfig = { // }, ], }, + { + title: "Examples", + items: [ + { + title: "Vercel Custom Domain", + href: "/docs/examples/vercel", + icon: "page", + }, + { + title: "Zeabur Custom Domain", + href: "/docs/examples/zeabur", + icon: "page", + }, + { + title: "Other", + href: "/docs/examples/other", + icon: "page", + }, + ], + }, { title: "Developer", items: [ diff --git a/content/docs/exanples/other.mdx b/content/docs/exanples/other.mdx new file mode 100644 index 0000000..bcc0082 --- /dev/null +++ b/content/docs/exanples/other.mdx @@ -0,0 +1,5 @@ +--- +title: Other +description: Free parsing custom domain names. +--- + diff --git a/content/docs/exanples/vercel.mdx b/content/docs/exanples/vercel.mdx new file mode 100644 index 0000000..e098da7 --- /dev/null +++ b/content/docs/exanples/vercel.mdx @@ -0,0 +1,5 @@ +--- +title: Vercel Custom Domain +description: Free parsing of Vercel custom domain names. +--- + diff --git a/content/docs/exanples/zeabur.mdx b/content/docs/exanples/zeabur.mdx new file mode 100644 index 0000000..55ae9d8 --- /dev/null +++ b/content/docs/exanples/zeabur.mdx @@ -0,0 +1,4 @@ +--- +title: Zeabur Custom Domain +description: Free parsing of Zeabur custom domain names. +--- \ No newline at end of file diff --git a/middleware.ts b/middleware.ts index fa49f81..ac04b5c 100644 --- a/middleware.ts +++ b/middleware.ts @@ -2,22 +2,37 @@ import { NextResponse } from "next/server"; import { auth } from "auth"; import { siteConfig } from "./config/site"; -import { createUserShortUrlMeta, getUrlBySuffix } from "./lib/dto/short-urls"; // export { auth as middleware } from "auth"; -// Or like this if you need to do something here. export default auth(async (req) => { - // console.log(req.auth); // { session: { user: { ... } } } + // console.log(req.auth); const ip = req.headers.get("X-Forwarded-For"); - // console.log("[middle/s]", ip); if (req.url.includes("/s/")) { - const slugRegex = /[^/]+$/; - const match = req.url.match(slugRegex); + const match = req.url.match(/[^/]+$/); + let geo = { + city: "", + region: "", + country: "", + latitude: "", + longitude: "", + }; + const data = await fetch(`https://ip.wr.do/api?ip=${ip}`); // http://ip-api.com/json/42.48.83.141 + if (data.ok) { + const geoData = await data.json(); + geo = { + city: geoData.city, + region: geoData.region, + country: geoData.country, + latitude: geoData.latitude, + longitude: geoData.longitude, + }; + } + if (match) { const res = await fetch( - `${siteConfig.url}/api/s?slug=${match[0]}&ip=${ip}`, + `${siteConfig.url}/api/s?slug=${match[0]}&ip=${ip}&city=${geo.city}®ion=${geo.region}&country=${geo.country}&latitude=${geo.latitude}&longitude=${geo.longitude}`, { method: "GET", },