Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Redis made optional #333

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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"
Expand All @@ -179,6 +179,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
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel this looks like a step of setting up upstash, since it is in the ordered list. I would probably place it above the ordered list so that people read it before setting up upstash.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Replace this line with the current content and place it on top of the bullet list, as a note when the user firstly starts reading this section:

> **Note** Upstash is used to provide rate-limiting capabilities to Noodle, which is optional when self hosting as that it's really only needed in the production version.


And that's all for the redis part!

Expand Down
4 changes: 2 additions & 2 deletions src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
OPENWEATHER_API_KEY: z.string().min(1),
},
Expand Down
28 changes: 18 additions & 10 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
Expand Down