-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmiddleware.ts
35 lines (29 loc) · 947 Bytes
/
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
import { NextRequest, NextResponse } from "next/server";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
const ratelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(5, "10 s"),
});
export default async function middleware(
request: NextRequest
): Promise<Response | undefined> {
if (request.url.endsWith("/create")) {
const ip = request.ip ?? "127.0.0.1";
const {success} = await ratelimit.limit(
ip
);
if (!success) return NextResponse.redirect(new URL("/blocked", request.url))
}
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-url', request.url);
return NextResponse.next({
request: {
headers: requestHeaders,
}
});
}