forked from mxkaske/mxkaske.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
UPSTASH_REDIS_REST_URL= | ||
UPSTASH_REDIS_REST_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Redis } from "@upstash/redis"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
const redis = Redis.fromEnv(); | ||
|
||
export const runtime = "edge"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const ip = request.ip; | ||
const searchParams = request.nextUrl.searchParams; | ||
const hasSlug = searchParams.has("slug"); | ||
const slug = hasSlug ? searchParams.get("slug") : null; | ||
|
||
if (ip) { | ||
const buf = await crypto.subtle.digest( | ||
"SHA-256", | ||
new TextEncoder().encode(ip) | ||
); | ||
|
||
const hash = Array.from(new Uint8Array(buf)) | ||
.map((b) => b.toString(16).padStart(2, "0")) | ||
.join(""); | ||
|
||
const isNew = await redis.set(["deduplicate", hash, slug].join(":"), true, { | ||
nx: true, | ||
ex: 24 * 60 * 60, // 1d | ||
}); | ||
|
||
if (!isNew) { | ||
new NextResponse("Already Increased Counter", { status: 200 }); | ||
} | ||
} | ||
|
||
await redis.incr(["pageviews", "posts", slug].join(":")); | ||
return new NextResponse("Increased Counter", { status: 202 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters