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

r010 [DPTM-12] #62

Merged
merged 10 commits into from
Aug 31, 2024
25 changes: 12 additions & 13 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { next } from '@vercel/edge';
import { ipAddress } from '@vercel/functions';
import { kv } from '@vercel/kv';
import { Ratelimit } from '@upstash/ratelimit';
// import { ipAddress } from '@vercel/functions';
// import { kv } from '@vercel/kv';
// import { Ratelimit } from '@upstash/ratelimit';

const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.slidingWindow(10, '3 s'),
});
// const ratelimit = new Ratelimit({
// redis: kv,
// limiter: Ratelimit.slidingWindow(10, '3 s'),
// });

export const config = {
matcher: ['/api/:path*'],
Expand All @@ -37,19 +37,18 @@ export default async function middleware(request: NextRequest) {
if (origin !== process.env.MAIN_URL) {
headers['Access-Control-Allow-Origin'] = allowedOrigins[origin] || 'https://www.dreampip.com';
}

// You could alternatively limit based on user ID or similar
const response = next();
const ip = ipAddress(request) || '127.0.0.1';

const { success, pending, limit, reset, remaining } = await ratelimit.limit(ip);
// You could alternatively limit based on user ID or similar
// const ip = ipAddress(request) || '127.0.0.1';
// const { success, pending, limit, reset, remaining } = await ratelimit.limit(ip);

if (!request?.url?.includes('auth')) {
Object.keys(headers).forEach((key: string) => {
response.headers.set(key, headers[key]);
});
}

return success ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
// return response ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
// return success ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
return response ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
}
Loading