Skip to content

Commit

Permalink
feat: Added a list of excluded IPs that don't get ratelimited
Browse files Browse the repository at this point in the history
  • Loading branch information
FleetAdmiralJakob committed Jan 21, 2024
1 parent 4df9eb2 commit 2c5893b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/api/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const env = createEnv({
UPSTASH_REDIS_REST_TOKEN: z.string().min(1),
UPSTASH_RATELIMITER_TOKENS_PER_TIME: z.string().min(1),
UPSTASH_RATELIMITER_TIME_INTERVAL: z.string().min(1),
UPSTASH_RATELIMITER_EXCLUDED_IPS: z.string().min(1),
RESEND_API_KEY: z.string().min(1),
QWEATHER_API_KEY: z.string().min(1),
API_NINJA_API_KEY: z.string().min(1)
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/routers/weather.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosResponse } from "axios";
import type { AxiosResponse } from "axios";
import axios from "axios";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ const ratelimit = new Ratelimit({
prefix: "@upstash/ratelimit",
});

const EXCLUDED_IPS = env.UPSTASH_RATELIMITER_EXCLUDED_IPS.split(",");

const rateLimitMiddleware = t.middleware(async ({ ctx, path, next }) => {
const identifier = `${ctx.ip}:${path}`;

if (EXCLUDED_IPS.includes(ctx.ip)) {
return next();
}

// log.debug("identifier", { identifier });
const { success, remaining } = await ratelimit.limit(identifier);
// log.debug("remaining", { remaining });
Expand Down
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"UPSTASH_REDIS_REST_TOKEN",
"UPSTASH_RATELIMITER_TOKENS_PER_TIME",
"UPSTASH_RATELIMITER_TIME_INTERVAL",
"UPSTASH_RATELIMITER_EXCLUDED_IPS",
"RESEND_API_KEY",
"QWEATHER_API_KEY",
"API_NINJA_API_KEY",
Expand Down

1 comment on commit 2c5893b

@vercel
Copy link

@vercel vercel bot commented on 2c5893b Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.