-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8b2b74
commit e2df3a0
Showing
18 changed files
with
246 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Redis } from '@upstash/redis'; | ||
|
||
const redis = Redis.fromEnv(); | ||
|
||
export default redis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { Ratelimit } from '@upstash/ratelimit'; | ||
import redis from './lib/redis'; | ||
|
||
export const config = { | ||
matcher: ['/api/:path*'], | ||
}; | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis, | ||
limiter: Ratelimit.slidingWindow(20, '10 s'), | ||
prefix: '@upstash/ratelimit', | ||
analytics: true, | ||
}); | ||
|
||
export async function middleware(request: NextRequest) { | ||
const ip = request.headers.get('x-forwarded-for') || '127.0.0.1'; | ||
const identifier = ip; | ||
|
||
let success, limit, remaining, reset; | ||
try { | ||
const result = await ratelimit.limit(identifier); | ||
success = result.success; | ||
limit = result.limit; | ||
remaining = result.remaining; | ||
reset = result.reset; | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ message: 'Internal Server Error' }, | ||
{ status: 500 }, | ||
); | ||
} | ||
|
||
if (!success) { | ||
return NextResponse.json( | ||
{ message: 'Rate limit exceeded', limit, remaining, reset }, | ||
{ status: 429 }, | ||
); | ||
} | ||
|
||
return NextResponse.next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
async function testRateLimit(url: string, attempts: number) { | ||
console.log(`Testing rate limit for ${url}`); | ||
for (let i = 0; i < attempts; i++) { | ||
const response = await fetch(url); | ||
const remaining = response.headers.get('X-RateLimit-Remaining'); | ||
console.log( | ||
`Attempt ${i + 1}: Status ${response.status}, Remaining: ${remaining}`, | ||
); | ||
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait 100ms between requests | ||
} | ||
console.log('\n'); | ||
} | ||
|
||
async function runTests() { | ||
const baseUrl = 'http://localhost:3000/api'; | ||
await testRateLimit(`${baseUrl}/price`, 25); | ||
await testRateLimit(`${baseUrl}/raffle`, 25); | ||
await testRateLimit(`${baseUrl}/raffle/luckyWinner`, 25); | ||
await testRateLimit(`${baseUrl}/referral/createUser`, 25); | ||
await testRateLimit(`${baseUrl}/stats`, 25); | ||
await testRateLimit(`${baseUrl}/stats/[address]`, 25); | ||
await testRateLimit(`${baseUrl}/strategies`, 25); | ||
await testRateLimit(`${baseUrl}/tnc/getUser`, 25); | ||
await testRateLimit(`${baseUrl}/tnc/getUser/[address]`, 25); | ||
await testRateLimit(`${baseUrl}/tnc/signUser`, 25); | ||
await testRateLimit(`${baseUrl}/users/ognft`, 25); | ||
await testRateLimit(`${baseUrl}/users/ognft/[address]`, 25); | ||
} | ||
|
||
runTests().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module 'lib/redis' { | ||
import { Redis } from '@upstash/redis'; | ||
const redis: Redis; | ||
export default redis; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "./dist", | ||
"rootDir": "./", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["scripts/**/*.ts"] | ||
} |
Oops, something went wrong.