Skip to content

Commit

Permalink
Fix env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-janon committed Jul 30, 2024
1 parent 4b604e5 commit 444f317
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,31 @@ const getPrompt = (userPrompt: string, pageSections: any[] | null) => {
// Allow streaming responses up to 45 seconds
export const maxDuration = 45;

const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.fixedWindow(5, "30s"),
});

export async function POST(req: Request) {
try {
const ip = req.ip ?? "ip";
const { success, remaining } = await ratelimit.limit(ip);
if (process.env.KV_REST_API_URL && process.env.KV_REST_API_TOKEN) {
const ip = req.headers.get("x-forwarded-for");
const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.fixedWindow(1, "10m"),
});

const { success, limit, reset, remaining } = await ratelimit.limit(`ratelimit_${ip}`);

if (!success) {
return new Response("You have reached your request limit for the day.", {
status: 429,
headers: {
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"X-RateLimit-Reset": reset.toString(),
},
});
}
} else {
throw new Error("KV_REST_API_URL and KV_REST_API_TOKEN env vars not found.");
}

const { messages: _messages } = await req.json();
const messages = _messages as CoreMessage[];

Expand Down

0 comments on commit 444f317

Please sign in to comment.