From 466525761b07d444a381af0112cfc7ae11aab316 Mon Sep 17 00:00:00 2001 From: YoussefLaunchUp <104530466+YoussefLaunchUp@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:24:59 -0400 Subject: [PATCH] feat: make redis optional --- .env.example | 2 +- CONTRIBUTING.md | 3 ++- src/env.mjs | 4 ++-- src/middleware.ts | 28 ++++++++++++++++++---------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 1700a3ad..cd387092 100644 --- a/.env.example +++ b/.env.example @@ -14,6 +14,6 @@ NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/app NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/app -# Redis (Upstash) +# Redis (Upstash) (optional) UPSTASH_REDIS_REST_URL= UPSTASH_REDIS_REST_TOKEN= \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7fa0d45f..96e4790b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -175,7 +175,7 @@ Clerk is our choice of authentication service for Noodle, it's true that it is a And now you got Auth! -#### Configuring Upstash +#### Configuring Upstash (optional) 1. Create your account through [Upstash's dashboard](https://console.upstash.com) 2. Click on "Create database" @@ -185,6 +185,7 @@ And now you got Auth! 6. In the "Connect to your database" section, select "@upstash/redis" 7. Copy the url into your `.env` file as `REDIS_URL` key 8. Copy the token into your `.env` file as `REDIS_TOKEN` key +9. Upstash is only used for ratelimiting, if left empty then ratelimiting will be disabled And that's all for the redis part! diff --git a/src/env.mjs b/src/env.mjs index 8d3a2ed9..80fbf7b0 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -9,8 +9,8 @@ export const env = createEnv({ NODE_ENV: z .enum(["development", "test", "production"]) .default("development"), - UPSTASH_REDIS_REST_URL: z.string().min(1), - UPSTASH_REDIS_REST_TOKEN: z.string().min(1), + UPSTASH_REDIS_REST_URL: z.string().min(1).optional(), + UPSTASH_REDIS_REST_TOKEN: z.string().min(0).optional(), CLERK_SECRET_KEY: z.string().min(1), }, client: { diff --git a/src/middleware.ts b/src/middleware.ts index 1b7ba0a3..2c0aca33 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,18 +1,23 @@ import { authMiddleware } from "@clerk/nextjs"; import { Ratelimit } from "@upstash/ratelimit"; import { Redis } from "@upstash/redis"; -import { type NextRequest, NextResponse } from "next/server"; +import { NextResponse, type NextRequest } from "next/server"; import { env } from "./env.mjs"; -const redis = new Redis({ - url: env.UPSTASH_REDIS_REST_URL, - token: env.UPSTASH_REDIS_REST_TOKEN, -}); +let redis: Redis; +let ratelimit: Ratelimit; -const ratelimit = new Ratelimit({ - redis: redis, - limiter: Ratelimit.slidingWindow(20, "3 s"), -}); +if (env.UPSTASH_REDIS_REST_URL) { + redis = new Redis({ + url: env.UPSTASH_REDIS_REST_URL ?? "", + token: env.UPSTASH_REDIS_REST_TOKEN ?? "", + }); + + ratelimit = new Ratelimit({ + redis: redis, + limiter: Ratelimit.slidingWindow(20, "3 s"), + }); +} async function rateLimitMiddleware( request: NextRequest, @@ -28,7 +33,10 @@ const publicRoutesThatShouldRedirectAfterAuth = ["/", "/waitlist"]; export default authMiddleware({ beforeAuth: (req) => { - return rateLimitMiddleware(req); + if (env.UPSTASH_REDIS_REST_URL) { + return rateLimitMiddleware(req); + } + return NextResponse.next(); }, afterAuth: (auth, req) => { if (