-
Notifications
You must be signed in to change notification settings - Fork 706
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
Showing
6 changed files
with
58 additions
and
45 deletions.
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
Empty file.
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,24 @@ | ||
import { ratelimit } from "./upstash"; | ||
|
||
export async function checkLicenseKey(licenseKey: string) { | ||
const response = await fetch(`https://api.lemonsqueezy.com/v1/license-keys`, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${process.env.LEMON_API_KEY ?? ""}`, | ||
}, | ||
}); | ||
const keysData = await response.json(); | ||
const licenseKeys = keysData.data?.map((i: any) => i.attributes.key); | ||
|
||
if (licenseKeys?.includes(licenseKey.toLowerCase())) { | ||
// TODO: change to supabase after implement user-login | ||
const { remaining } = await ratelimit.limit(licenseKey.toLowerCase()); | ||
// TODO: log to hit licenseKey | ||
console.log( | ||
`!!!!!!!!! {short-xxxx-licenseKey}, remaining: ${remaining} ========` | ||
); | ||
return remaining > 0; | ||
} | ||
|
||
return false; | ||
} |
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,4 @@ | ||
export function checkOpenaiApiKey(str: string) { | ||
var pattern = /^sk-[A-Za-z0-9]{48}$/; | ||
return pattern.test(str); | ||
} |
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 "../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