Skip to content

Commit

Permalink
add rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsv authored and cgsv committed Mar 7, 2023
1 parent a8f05da commit 18f38c0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .example.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
OPENAI_API_KEY=sk-xxx
BILIBILI_SESSION_TOKEN=
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
UPSTASH_REDIS_REST_TOKEN=
UPSTASH_RATE_REDIS_REST_URL=
UPSTASH_RATE_REDIS_REST_TOKEN=
13 changes: 13 additions & 0 deletions lib/upstash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { RATE_LIMIT_COUNT } from "../utils/constants";

export const ratelimit = new Ratelimit({
redis: new Redis({
url: process.env.UPSTASH_RATE_REDIS_REST_URL,
token: process.env.UPSTASH_RATE_REDIS_REST_TOKEN,
}),
// 速率限制算法 https://github.com/upstash/ratelimit#ratelimiting-algorithms
limiter: Ratelimit.fixedWindow(RATE_LIMIT_COUNT, "1 d"),
analytics: true, // <- Enable analytics
});
42 changes: 42 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
// import { validateLicenseKey } from "./lib/lemon";
import { checkOpenaiApiKeys } from "./lib/openai/openai";
import { ratelimit } from "./lib/upstash"
import { isDev } from "./utils/env";

const redis = Redis.fromEnv();

export async function middleware(req: NextRequest, context: NextFetchEvent) {
const { apiKey } = await req.json();
// const result = await redis.get<string>(bvId);
// if (!isDev && result) {
// console.log("hit cache for ", bvId);
// return NextResponse.json(result);
// }

// licenseKeys
if (apiKey) {
if (checkOpenaiApiKeys(apiKey)) {
return NextResponse.next();
}

// // 3. something-invalid-sdalkjfasncs-key
// if (!(await validateLicenseKey(apiKey, bvId))) {
// return NextResponse.redirect(new URL("/shop", req.url));
// }
}
// TODO: unique to a user (userid, email etc) instead of IP
const identifier = req.ip ?? "127.0.0.7";
const { success, remaining } = await ratelimit.limit("trans-" + identifier);
console.log(`======== ip ${identifier}, remaining: ${remaining} ========`);
if (!apiKey && !success) {
// return NextResponse.redirect(new URL("/shop", req.url));
return NextResponse.error();
}
}

export const config = {
matcher: "/api/translate",
};
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/node": "18.14.4",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
"@upstash/ratelimit": "^0.3.9",
"@upstash/redis": "^1.20.1",
"detect-file-encoding-and-language": "^2.3.2",
"eslint": "8.35.0",
Expand Down

0 comments on commit 18f38c0

Please sign in to comment.