forked from vercel/examples
-
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.
Add/update rate limiting examples with Vercel KV (vercel#745)
- Loading branch information
Showing
46 changed files
with
912 additions
and
808 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
UPSTASH_REST_API_DOMAIN = | ||
UPSTASH_REST_API_TOKEN = | ||
KV_URL="redis://..." | ||
KV_REST_API_URL="https://..." | ||
KV_REST_API_TOKEN="AXx3ASQ..." | ||
KV_REST_API_READ_ONLY_TOKEN="Anx3ASQ..." |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
import type { NextRequest } from 'next/server' | ||
import { ipRateLimit } from '@lib/ip-rate-limit' | ||
import { Ratelimit } from '@upstash/ratelimit' | ||
import { kv } from '@vercel/kv' | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis: kv, | ||
// 5 requests from the same IP in 10 seconds | ||
limiter: Ratelimit.slidingWindow(5, '10 s'), | ||
}) | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default async function handler(req: NextRequest) { | ||
const res = await ipRateLimit(req) | ||
// If the status is not 200 then it has been rate limited. | ||
if (res.status !== 200) return res | ||
|
||
res.headers.set('content-type', 'application/json') | ||
export default async function handler(request: NextRequest) { | ||
// You could alternatively limit based on user ID or similar | ||
const ip = request.ip ?? '127.0.0.1' | ||
const { limit, reset, remaining } = await ratelimit.limit(ip) | ||
|
||
return new Response(JSON.stringify({ success: true }), { | ||
status: 200, | ||
headers: res.headers, | ||
headers: { | ||
'X-RateLimit-Limit': limit.toString(), | ||
'X-RateLimit-Remaining': remaining.toString(), | ||
'X-RateLimit-Reset': reset.toString(), | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.