- Real Grid 컴포넌트 @rsone/components 로 모듈화 - 컴포넌트 동적 import 경로 수정(engName -> urlPath)
143 lines
4.9 KiB
TypeScript
143 lines
4.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import {
|
|
CLAIM_NAME,
|
|
AUTH_USER_ID,
|
|
PORT,
|
|
PROXY_HOST,
|
|
SERVER_API_URL,
|
|
SITE_ID,
|
|
ST_SERVICE_URL,
|
|
} from "./src/shared/config";
|
|
|
|
const serverApiUrl =
|
|
process.env.NEXT_PUBLIC_SERVER_API_URL ?? "http://localhost:3000";
|
|
|
|
const linkBackendApiHost = process.env.NEXT_PUBLIC_LINK_BACKEND_API_HOST ?? "http://localhost:8080";
|
|
|
|
const stServiceUrl = ST_SERVICE_URL;
|
|
|
|
// 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("/st-management-service") || pathname.startsWith("/st-input-service")) {
|
|
const requestHeaders = new Headers(req.headers);
|
|
const token = req.cookies.get(CLAIM_NAME)?.value;
|
|
const userId = req.cookies.get(AUTH_USER_ID)?.value;
|
|
|
|
const queryParams = searchParams.toString();
|
|
const newUrl = `${stServiceUrl}${pathname}${
|
|
queryParams ? `?${queryParams}` : ""
|
|
}`;
|
|
|
|
if (token) requestHeaders.set(CLAIM_NAME, token);
|
|
if (userId) requestHeaders.set(AUTH_USER_ID, userId);
|
|
|
|
// 디버깅을 위한 토큰 정보 로그
|
|
logMessages.push(` Token Debug: ${token ? 'EXISTS' : 'MISSING'}`);
|
|
logMessages.push(` UserId Debug: ${userId ? 'EXISTS' : 'MISSING'}`);
|
|
logMessages.push(` ST Service Rewritten URL: ${newUrl}`);
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.rewrite(new URL(newUrl), {
|
|
headers: requestHeaders,
|
|
});
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
if (pathname.startsWith("/server")) {
|
|
const requestHeaders = new Headers(req.headers);
|
|
const token = req.cookies.get(CLAIM_NAME)?.value;
|
|
const userId = req.cookies.get(AUTH_USER_ID)?.value;
|
|
|
|
const queryParams = searchParams.toString();
|
|
const newUrl = `${serverApiUrl}${pathname.replace("/server", "")}${
|
|
queryParams ? `?${queryParams}` : ""
|
|
}`;
|
|
|
|
if (token) requestHeaders.set(CLAIM_NAME, token);
|
|
if (userId) requestHeaders.set(AUTH_USER_ID, userId);
|
|
|
|
logMessages.push(` Rewritten URL: ${newUrl}`);
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.rewrite(new URL(newUrl), {
|
|
headers: requestHeaders,
|
|
});
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
if (pathname.startsWith("/attach-service")) {
|
|
const requestHeaders = new Headers(req.headers);
|
|
const token = req.cookies.get(CLAIM_NAME)?.value;
|
|
const userId = req.cookies.get(AUTH_USER_ID)?.value;
|
|
const newUrl = `${serverApiUrl}${pathname}`;
|
|
|
|
if (token) requestHeaders.set(CLAIM_NAME, token);
|
|
if (userId) requestHeaders.set(AUTH_USER_ID, userId);
|
|
|
|
logMessages.push(` Rewritten URL: ${newUrl}`);
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.rewrite(new URL(newUrl), {
|
|
headers: requestHeaders,
|
|
});
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
if (pathname.startsWith("/api/link")) {
|
|
const requestHeaders = new Headers(req.headers);
|
|
const token = req.cookies.get(CLAIM_NAME)?.value;
|
|
const userId = req.cookies.get(AUTH_USER_ID)?.value;
|
|
|
|
// /api/link 경로를 제거하고 백엔드 API 경로로 변환
|
|
const apiPath = pathname.replace("/api/link", "");
|
|
const queryParams = searchParams.toString();
|
|
const newUrl = `${linkBackendApiHost}${apiPath}${
|
|
queryParams ? `?${queryParams}` : ""
|
|
}`;
|
|
|
|
if (token) requestHeaders.set(CLAIM_NAME, token);
|
|
if (userId) requestHeaders.set(AUTH_USER_ID, userId);
|
|
|
|
logMessages.push(` Link API Rewritten URL: ${newUrl}`);
|
|
logMessages.push("\n--------------------------------\n");
|
|
console.log(logMessages.join("\n"));
|
|
|
|
const response = NextResponse.rewrite(new URL(newUrl), {
|
|
headers: requestHeaders,
|
|
});
|
|
return addSecurityHeaders(response);
|
|
}
|
|
|
|
if (pathname === "/") {
|
|
const response = NextResponse.redirect(new URL("/statistics", 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*", "/attach-service/:path*", "/api/link/:path*", "/st-management-service/:path*", "/st-input-service/:path*", "/"],
|
|
};
|