-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ratelimit on both api handlers and TRPC route on user route (#30)
* added upstash redis and .env.example * adding server-side-env and redis client on /ratelimit * adding sliding window approach * add new envs to git workflow * fix t3-env error --------- Co-authored-by: Touha Zohair <[email protected]>
- Loading branch information
Showing
9 changed files
with
113 additions
and
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {Ratelimit} from "@upstash/ratelimit"; | ||
import {Redis} from "@upstash/redis"; | ||
|
||
// creating redis client | ||
const redis = new Redis({ | ||
url: 'UPSTASH_REDIS_REST_URL', | ||
token: 'UPSTASH_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
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,9 @@ | ||
import { Ratelimit } from "@upstash/ratelimit" | ||
import { Redis } from "@upstash/redis" | ||
|
||
export const ratelimit = new Ratelimit({ | ||
redis:Redis.fromEnv(), | ||
// using sliding window approach - found out more - https://github.com/upstash/ratelimit#sliding-window | ||
limiter:Ratelimit.slidingWindow(1 , "10 m") | ||
|
||
}) |
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,44 @@ | ||
import { ratelimit } from "@/lib/ratelimit"; | ||
|
||
export async function GET(req: Request ) { | ||
try { | ||
// const res = await req.() | ||
|
||
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(JSON.stringify({status:"OK"})) | ||
|
||
}catch(err) { | ||
console.log('Error has occured : ' , err) | ||
} | ||
} | ||
|
||
|
||
export async function POST(req: Request ) { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const res = await req.json() | ||
|
||
const ip = req.headers.get('x-forwarded-for') ?? '' | ||
// you can use the response or the user id here instead of ip - | ||
// const {id} = res - by sending as the user id as a post requst | ||
const data = await ratelimit.limit(ip) | ||
if(!data.success) { | ||
return new Response("You can only send 1 req / 30min.", { | ||
status: 429, | ||
}) | ||
} | ||
|
||
return new Response(JSON.stringify({status:"OK" , ...res})) | ||
|
||
}catch(err) { | ||
console.log('Error has occured : ' , err) | ||
} | ||
} |
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,5 +1,11 @@ | ||
import { ratelimit } from "@/lib/ratelimit"; | ||
import { protectedProcedure, createTRPCRouter } from "../trpc"; | ||
|
||
import { TRPCError } from "@trpc/server"; | ||
export const userRouter = createTRPCRouter({ | ||
get: protectedProcedure.query(({ ctx }) => ctx.user), | ||
}); | ||
get: protectedProcedure.query(async ({ ctx }) => { | ||
const {success} = await ratelimit.limit(ctx.user.id) | ||
if(!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" }); | ||
return ctx.user | ||
|
||
}), | ||
}); |