-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cgsv
authored and
cgsv
committed
Mar 7, 2023
1 parent
a8f05da
commit 18f38c0
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters