51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { PORT, PROXY_HOST, SERVER_API_URL, SITE_ID } from "@/shared/config";
|
|
|
|
const serverApiUrl = process.env.NEXT_PUBLIC_SERVER_API_URL ?? "http://localhost:3000";
|
|
|
|
// CSP 헤더 설정 함수 (img-src만)
|
|
function addSecurityHeaders(response: NextResponse): NextResponse {
|
|
response.headers.set("Content-Security-Policy", "img-src 'self' data: https: blob:;");
|
|
return response;
|
|
}
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const pathname = req.nextUrl.pathname;
|
|
const searchParams = req.nextUrl.searchParams;
|
|
|
|
const logMessages: string[] = [
|
|
`[Middleware Request] ${new Date().toISOString()}`,
|
|
`[Environment]\n Mode: ${process.env.NODE_ENV}\n Port: ${PORT}\n Proxy Host: ${PROXY_HOST}\n Server API URL: ${SERVER_API_URL}\n Site ID: ${SITE_ID}`,
|
|
`[Request]\n Original Pathname: ${pathname}`,
|
|
];
|
|
|
|
if (pathname.startsWith("/server")) {
|
|
const queryParams = searchParams.toString();
|
|
const newUrl = `${serverApiUrl}${pathname.replace("/server", "")}${
|
|
queryParams ? `?${queryParams}` : ""
|
|
}`;
|
|
|
|
logMessages.push(` Rewritten URL: ${newUrl}`);
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.rewrite(new URL(newUrl));
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
// if (pathname === "/") {
|
|
// const response = NextResponse.redirect(new URL("/dashboard", req.url));
|
|
// return addSecurityHeaders(response);
|
|
// }
|
|
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.next();
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/server/:path*", "/"],
|
|
};
|