-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
69 lines (57 loc) · 1.79 KB
/
middleware.ts
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
import { NextRequest, NextResponse } from 'next/server'
import { Ratelimit } from '@upstash/ratelimit'
import { kv } from '@vercel/kv'
const LOCAL_IP = '127.0.0.1'
const ratelimit = new Ratelimit({
redis: kv,
// 5 requests from the same IP in 10 seconds
limiter: Ratelimit.slidingWindow(5, '10 s')
})
// CORS headers for the api endpoints
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
export async function middleware(request: NextRequest) {
// maintenance mode handler
if (process.env.MAINTENANCE_MODE === 'ON') {
request.nextUrl.pathname = `/maintenance`
return NextResponse.rewrite(request.nextUrl)
}
const response = NextResponse.next()
// CORS handler
if (request.method === 'OPTIONS') {
return NextResponse.json(null, { status: 204, headers: corsHeaders })
}
// rate limiter handler
const ip = request.ip ?? LOCAL_IP
if (ip !== '127.0.0.1') {
const { success, limit, reset, remaining } = await ratelimit.limit(
`koinos_rest_ratelimit_${ip}`
)
if (!success) {
return NextResponse.json(null, {
status: 429,
headers: {
'X-RateLimit-Limit': limit.toString(),
'X-RateLimit-Remaining': remaining.toString(),
'X-RateLimit-Reset': reset.toString()
}
})
}
}
response.headers.append('Access-Control-Allow-Origin', corsHeaders['Access-Control-Allow-Origin'])
response.headers.append(
'Access-Control-Allow-Methods',
corsHeaders['Access-Control-Allow-Methods']
)
response.headers.append(
'Access-Control-Allow-Headers',
corsHeaders['Access-Control-Allow-Headers']
)
return response
}
export const config = {
matcher: '/api/:path*'
}