-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
70 lines (62 loc) · 2.12 KB
/
route.ts
File metadata and controls
70 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { NextRequest, NextResponse } from 'next/server';
import { searchRateLimiter, getClientIdentifier } from '@/lib/rate-limit';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const detailed = searchParams.get('detailed') === 'true';
const clientId = searchParams.get('client');
if (clientId) {
// Get stats for specific client
const clientInfo = searchRateLimiter.getClientInfo(clientId);
return NextResponse.json({
client: clientInfo,
timestamp: new Date().toISOString(),
});
}
const stats = detailed ? searchRateLimiter.getDetailedStats() : searchRateLimiter.getStats();
return NextResponse.json({
rateLimiting: stats,
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error('Rate limit stats error:', error);
return NextResponse.json(
{ error: 'Failed to retrieve rate limit statistics' },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { action, clientId, additionalRequests, durationMs } = body;
if (action === 'reset' && clientId) {
const reset = searchRateLimiter.resetClient(clientId);
return NextResponse.json({
success: reset,
message: reset ? 'Client rate limit reset' : 'Client not found',
clientId,
});
}
if (action === 'increase' && clientId && additionalRequests) {
searchRateLimiter.increaseLimit(clientId, additionalRequests, durationMs);
return NextResponse.json({
success: true,
message: `Increased limit for client by ${additionalRequests} requests`,
clientId,
additionalRequests,
durationMs: durationMs || 'default',
});
}
return NextResponse.json(
{ error: 'Invalid action or missing parameters' },
{ status: 400 }
);
} catch (error) {
console.error('Rate limit management error:', error);
return NextResponse.json(
{ error: 'Failed to manage rate limits' },
{ status: 500 }
);
}
}