From 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b Mon Sep 17 00:00:00 2001 From: Fuwn <50817549+Fuwn@users.noreply.github.com> Date: Sat, 24 Jan 2026 13:09:50 +0000 Subject: Initial commit Created from https://vercel.com/new --- src/app/(collect)/p/[slug]/route.ts | 68 +++++++++++++++++++++++++++++++++++++ src/app/(collect)/q/[slug]/route.ts | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/app/(collect)/p/[slug]/route.ts create mode 100644 src/app/(collect)/q/[slug]/route.ts (limited to 'src/app/(collect)') diff --git a/src/app/(collect)/p/[slug]/route.ts b/src/app/(collect)/p/[slug]/route.ts new file mode 100644 index 0000000..79d6faa --- /dev/null +++ b/src/app/(collect)/p/[slug]/route.ts @@ -0,0 +1,68 @@ +export const dynamic = 'force-dynamic'; + +import { NextResponse } from 'next/server'; +import { POST } from '@/app/api/send/route'; +import type { Pixel } from '@/generated/prisma/client'; +import redis from '@/lib/redis'; +import { notFound } from '@/lib/response'; +import { findPixel } from '@/queries/prisma'; + +const image = Buffer.from('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw', 'base64'); + +export async function GET(request: Request, { params }: { params: Promise<{ slug: string }> }) { + const { slug } = await params; + + let pixel: Pixel; + + if (redis.enabled) { + pixel = await redis.client.fetch( + `pixel:${slug}`, + async () => { + return findPixel({ + where: { + slug, + }, + }); + }, + 86400, + ); + + if (!pixel) { + return notFound(); + } + } else { + pixel = await findPixel({ + where: { + slug, + }, + }); + + if (!pixel) { + return notFound(); + } + } + + const payload = { + type: 'event', + payload: { + pixel: pixel.id, + url: request.url, + referrer: request.headers.get('referer'), + }, + }; + + const req = new Request(request.url, { + method: 'POST', + headers: request.headers, + body: JSON.stringify(payload), + }); + + await POST(req); + + return new NextResponse(image, { + headers: { + 'Content-Type': 'image/gif', + 'Content-Length': image.length.toString(), + }, + }); +} diff --git a/src/app/(collect)/q/[slug]/route.ts b/src/app/(collect)/q/[slug]/route.ts new file mode 100644 index 0000000..24089bd --- /dev/null +++ b/src/app/(collect)/q/[slug]/route.ts @@ -0,0 +1,61 @@ +export const dynamic = 'force-dynamic'; + +import { NextResponse } from 'next/server'; +import { POST } from '@/app/api/send/route'; +import type { Link } from '@/generated/prisma/client'; +import redis from '@/lib/redis'; +import { notFound } from '@/lib/response'; +import { findLink } from '@/queries/prisma'; + +export async function GET(request: Request, { params }: { params: Promise<{ slug: string }> }) { + const { slug } = await params; + + let link: Link; + + if (redis.enabled) { + link = await redis.client.fetch( + `link:${slug}`, + async () => { + return findLink({ + where: { + slug, + }, + }); + }, + 86400, + ); + + if (!link) { + return notFound(); + } + } else { + link = await findLink({ + where: { + slug, + }, + }); + + if (!link) { + return notFound(); + } + } + + const payload = { + type: 'event', + payload: { + link: link.id, + url: request.url, + referrer: request.headers.get('referer'), + }, + }; + + const req = new Request(request.url, { + method: 'POST', + headers: request.headers, + body: JSON.stringify(payload), + }); + + await POST(req); + + return NextResponse.redirect(link.url); +} -- cgit v1.2.3