29 lines
823 B
TypeScript
29 lines
823 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
const serverApiUrl =
|
|
process.env.NEXT_PUBLIC_SERVER_API_URL ?? "http://localhost:3000";
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const pathname = req.nextUrl.pathname;
|
|
const searchParams = req.nextUrl.searchParams;
|
|
|
|
if (pathname.startsWith("/server")) {
|
|
const queryParams = searchParams.toString();
|
|
const newUrl = `${serverApiUrl}${pathname.replace("/server", "")}${
|
|
queryParams ? `?${queryParams}` : ""
|
|
}`;
|
|
return NextResponse.rewrite(new URL(newUrl));
|
|
}
|
|
|
|
if (pathname.startsWith("/attach-service")) {
|
|
const newUrl = `${serverApiUrl}${pathname}`;
|
|
return NextResponse.rewrite(new URL(newUrl));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/server/:path*", "/attach-service/:path*"],
|
|
};
|