plebdevs/src/middleware.js

34 lines
829 B
JavaScript
Raw Normal View History

import { NextResponse } from 'next/server';
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'),
2024-09-30 19:51:23 -05:00
analytics: true,
timeout: 1000, // 1 second
});
// Define which routes you want to rate limit
export const config = {
2024-09-30 19:55:56 -05:00
matcher: '/api/:path*',
};
export default async function middleware(request) {
const ip = request.ip ?? '127.0.0.1';
const { success, pending, limit, reset, remaining } = await ratelimit.limit(
2024-09-30 19:51:23 -05:00
`ratelimit_middleware_${ip}`
);
2024-09-30 19:51:23 -05:00
if (!success) {
return new NextResponse('Too Many Requests', {
status: 429,
headers: {
'Retry-After': reset.toString(),
},
});
}
return NextResponse.next();
}