forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
33 lines (29 loc) · 1.06 KB
/
Copy pathroute.ts
File metadata and controls
33 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { NextResponse } from "next/server";
import { rateLimit, logSecurityEvent } from "@/lib/security";
export async function POST(request: Request) {
try {
const ip = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "unknown";
const rl = rateLimit(`analytics:${ip}`, 60, 60_000);
if (!rl.allowed) {
logSecurityEvent("rate_limited", { ip, endpoint: "analytics" });
return NextResponse.json({ success: false }, { status: 429 });
}
const body = await request.json();
const { url, referrer, ts } = body;
// In production, this would write to a database or analytics service.
// For now, we log to server console (Vercel logs) for visibility.
console.log(
JSON.stringify({
type: "pageview",
url: url || "/",
referrer: referrer || "",
ts: ts || Date.now(),
ip,
userAgent: request.headers.get("user-agent") || "unknown",
})
);
return NextResponse.json({ success: true });
} catch {
return NextResponse.json({ success: false }, { status: 400 });
}
}