forked from team-headstart/ai-answer-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
61 lines (50 loc) · 1.8 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// TODO: Implement the code here to add rate limiting with Redis
// Refer to the Next.js Docs: https://nextjs.org/docs/app/building-your-application/routing/middleware
// Refer to Redis docs on Rate Limiting: https://upstash.com/docs/redis/sdks/ratelimit-ts/algorithms
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { Redis } from "@upstash/redis";
import { Ratelimit } from "@upstash/ratelimit";
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
})
const rateLimit = new Ratelimit({
redis: redis,
// 10 requests per 60 seconds
limiter: Ratelimit.slidingWindow(10, "60 s"),
analytics: true,
})
export async function middleware(request: NextRequest) {
try {
// get the user's IP address from request header
const ip = (request.headers.get("x-real-ip") || request.headers.get("x-forwarded-for")) ?? '127.0.0.1';
const { success, limit, reset, remaining } = await rateLimit.limit(ip);
const response = success
? NextResponse.next()
: NextResponse.json(
{ error: "Too many requests. Please try again later." },
{ status: 429 }
)
response.headers.set("X-RateLimit-Limit", limit.toString());
response.headers.set("X-RateLimit-Remaining", remaining.toString());
response.headers.set("X-RateLimit-Reset", reset.toString());
return response
} catch (error) {
// Handle the error appropriately
console.error(error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
// Configure which paths the middleware runs on
export const config = {
matcher: [
/*
* Match all request paths except static files and images
*/
"/((?!_next/static|_next/image|favicon.ico).*)",
],
};