-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
59 lines (53 loc) · 1.66 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
import { Ratelimit } from '@upstash/ratelimit';
import { kv } from '@vercel/kv';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
const ratelimit = new Ratelimit({
redis: kv as any,
// 5 requests from the same IP in 10 seconds
limiter: Ratelimit.slidingWindow(200, '10 s')
});
export async function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/api/x1') && process.env.X1_RPC_URL) {
const body = await request.json();
return fetch(process.env.X1_RPC_URL, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
}
});
}
if (request.nextUrl.pathname.startsWith('/api/xenblocks')) {
const path = request.nextUrl.pathname.replace('/api/xenblocks', '');
const url = `http://xenblocks.io:4447${path}`;
// console.log(request.nextUrl.pathname, path, url);
return NextResponse.rewrite(url, {
headers: {
'Content-Type': 'application/json'
}
});
}
if (request.nextUrl.pathname === '/') {
return NextResponse.redirect(`${request.nextUrl.origin}/x1/moon-party`);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: '/((?!_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' }
]
}
]
};