generated from replicate/getting-started-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from replicate/rate-limit
Add rate limiting for API endpoints
- Loading branch information
Showing
4 changed files
with
149 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,36 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { Ratelimit } from '@upstash/ratelimit'; | ||
import { kv } from '@vercel/kv'; | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis: kv, | ||
// 20 requests from the same IP within a 10 second sliding window | ||
limiter: Ratelimit.slidingWindow(20, '10s'), | ||
prefix: `v2/zoo/ratelimit/${process.env.VERCEL_ENV ?? 'local'}`, | ||
timeout: 500, | ||
}); | ||
|
||
// Rate limit the /api/predictions/[id] endpoint | ||
export const config = { | ||
matcher: ['/api/predictions/:path+'], | ||
}; | ||
|
||
export default async function middleware(request: NextRequest) { | ||
if (!process.env.VERCEL_ENV || !process.env.KV_REST_API_URL || !process.env.KV_REST_API_URL) { | ||
console.warn('Skipping ratelimiting middleware'); | ||
return NextResponse.next(); | ||
} | ||
|
||
const ip = request.ip ?? '127.0.0.1'; | ||
const { success, limit, remaining, reset } = await ratelimit.limit(ip); | ||
const headers = { | ||
'X-Ratelimit-Hit': String(!success), | ||
'X-Ratelimit-Limit': String(limit), | ||
'X-Ratelimit-Remaining': String(remaining), | ||
'X-Ratelimit-Reset': String(reset), | ||
} | ||
|
||
return success | ||
? NextResponse.next({headers}) | ||
: NextResponse.json({}, { status: 429, headers }); | ||
} |
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
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
0091e33
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
zoo – ./
zoo-git-main-replicate.vercel.app
cat-rose.vercel.app
zoo.replicate.dev
zoo-replicate.vercel.app