-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from Kinfe123/feat/ratelimit
fix: add ratelimter
- Loading branch information
Showing
8 changed files
with
67 additions
and
8 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
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,17 @@ | ||
import { ratelimit } from "utils/ratelimit"; | ||
export async function POST(req: Request) { | ||
try { | ||
const res = await req.json(); | ||
const ip = req.headers.get("x-forwarded-for") ?? ""; | ||
const data = await ratelimit.limit(ip); | ||
if (!data.success) { | ||
return new Response("You can only send 1 req / 30min.", { | ||
status: 429, | ||
}); | ||
} | ||
return new Response("The data logged: " + JSON.stringify(res)); | ||
} catch (err) { | ||
console.log("Error has occured ", err); | ||
return new Response("Error has occured", { status: 404 }); | ||
} | ||
} |
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
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
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" | ||
export const db: Redis = new Redis({ | ||
url: process.env.UPSTASH_REDIS_REST_URL!, | ||
token: process.env.UPSTASH_REDIS_REST_TOKEN! | ||
}); | ||
|
||
export const ratelimit = new Ratelimit({ | ||
redis: db, | ||
limiter: Ratelimit.slidingWindow(1, "30 m") | ||
|
||
}) | ||
|
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.