Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactored redis

Steven Tey723eabf40b02
diff --git a/lib/constants.ts b/lib/constants.ts
new file mode 100644
index 0000000000..2e42d77bf8
--- /dev/null
+++ b/lib/constants.ts
@@ -0,0 +1,7 @@
+export const LOCALHOST_GEO_DATA = {
+ city: "San Francisco",
+ region: "CA",
+ country: "US",
+ latitude: "37.7695",
+ longitude: "-122.385",
+};
diff --git a/middleware.ts b/middleware.ts
index 78c61af0ba..fd834ac51e 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -1,19 +1,38 @@
-import { NextRequest, NextFetchEvent, NextResponse } from "next/server";
+import {
+ NextRequest,
+ NextFetchEvent,
+ NextResponse,
+ userAgent,
+} from "next/server";
import { redis } from "@/lib/redis";
+import { LOCALHOST_GEO_DATA } from "@/lib/constants";
export const config = {
matcher: "/([^/.]*)",
};
export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
+ let hostname = req.headers.get("host");
+ if (hostname === "localhost:3000") hostname = "dub.sh";
+
const path = req.nextUrl.pathname;
const key = path.split("/")[1];
if (key.length === 0) {
return NextResponse.next();
} // skip root
- const target = await redis.hget<string>("links", key);
+
+ const target = await redis.hget<string>(`${hostname}:links`, key);
if (target) {
- ev.waitUntil(redis.hincrby("stats", key, 1)); // increment click count
+ ev.waitUntil(
+ redis.zadd(`${hostname}:${key}:clicks`, {
+ score: Date.now(),
+ member: {
+ geo: process.env.VERCEL === "1" ? req.geo : LOCALHOST_GEO_DATA,
+ ua: userAgent(req),
+ timestamp: Date.now(),
+ },
+ })
+ ); // increment click count
return NextResponse.redirect(target);
} else {
const url = req.nextUrl.clone();
diff --git a/pages/api/links/[key]/clicks.ts b/pages/api/links/[key]/clicks.ts
index 527e8b623b..5c70383c2a 100644
--- a/pages/api/links/[key]/clicks.ts
+++ b/pages/api/links/[key]/clicks.ts
@@ -9,7 +9,7 @@ export default async function handler(req: NextRequest) {
if (req.method === "GET") {
const url = req.nextUrl.pathname;
const key = url.split("/")[3];
- const response = (await redis.hget("stats", key)) || "0";
+ const response = (await redis.zcard(`dub.sh:${key}:clicks`)) || "0";
return new Response(JSON.stringify(response), { status: 200 });
} else {
return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index af8e1e32d4..597ce55856 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -9,14 +9,13 @@ export const config = {
export default async function handler(req: NextRequest) {
if (req.method === "POST") {
const url = req.nextUrl.searchParams.get("url");
+ const hostname = req.nextUrl.searchParams.get("hostname");
const nanoid = customAlphabet(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7
- ); // 7-character random string (similar to bit.ly)
+ ); // 7-character random string
const key = nanoid(); //
- const response = await redis.hset("links", {
- [key]: url,
- });
+ const response = await redis.hsetnx(`${hostname}:links`, key, url);
if (response === 1) {
return new Response(
JSON.stringify({
diff --git a/pages/index.tsx b/pages/index.tsx
index 5fe286a822..d0d3b1c440 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -36,7 +36,7 @@ export default function Home() {
onSubmit={async (e) => {
e.preventDefault();
setSaving(true);
- fetch(`/api/links?url=${url}`, {
+ fetch(`/api/links?url=${url}&hostname=dub.sh`, {
method: "POST",
headers: {
"Content-Type": "application/json",