forked from UNICKCHENG/openai-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
63 lines (53 loc) · 2.11 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
62
63
import { Ratelimit } from "@upstash/ratelimit"
import { Redis } from "@upstash/redis"
import { type NextFetchEvent, type NextRequest, NextResponse } from "next/server"
import { url } from "@/libs/utils/url"
const initRateLimit = () => {
if (process.env.UPSTASH_REDIS_REST_TOKEN) {
return new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.cachedFixedWindow(Number(process.env.UPSTASH_RATE_LIMIT), "60s"),
ephemeralCache: new Map(),
analytics: true,
});
}
return undefined;
}
const ratelimit: Ratelimit | undefined = initRateLimit();
const corsHeaders = ({
"Access-Control-Allow-Origin": process.env.ACCESS_CONTROL_ALLOW_ORIGIN!,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Accept, Authorization",
});
const setCorsHeaders = (res: NextResponse) => {
Object.keys(corsHeaders).forEach(key => {
res.headers.append(key, corsHeaders[key]);
})
return res;
}
export async function middleware(
request: NextRequest,
event: NextFetchEvent
): Promise<Response | undefined> {
if ('OPTIONS' == request.method) {
return new Response(null, { status: 200, headers: corsHeaders });
}
if (request.nextUrl.pathname === '/') {
return NextResponse.redirect(new URL('https://github.com/UNICKCHENG/openai-proxy', request.url));
}
if (ratelimit) {
const ip: String = request.ip ?? "127.0.0.1";
const { success, pending, limit, reset, remaining } = await ratelimit.limit(`ratelimit_middleware_${ip}`);
event.waitUntil(pending);
const res: NextResponse = success ? NextResponse.next() : NextResponse.rewrite(url('/api/blocked', request));
res.headers.set("X-RateLimit-Limit", limit.toString());
res.headers.set("X-RateLimit-Remaining", remaining.toString());
res.headers.set("X-RateLimit-Reset", reset.toString());
return setCorsHeaders(res);
}
const response = NextResponse.next();
return setCorsHeaders(response);
}
export const config = {
matcher: '/:path*',
}