generated from replicate/getting-started-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathmiddleware.ts
39 lines (33 loc) · 1.2 KB
/
middleware.ts
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
34
35
36
37
38
39
import { NextRequest, NextResponse } from 'next/server';
import { Ratelimit } from '@upstash/ratelimit';
import { kv } from '@vercel/kv';
const ephemeralCache = new Map();
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,
ephemeralCache,
});
// 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 });
}