import { NextRequest, NextResponse } from "next/server"; import { CLAIM_NAME, AUTH_USER_ID } from "./src/shared/config"; // import { SERVER_API_FO_URL, SERVER_API_WR_URL } from "./src/shared/config/env"; // const CLAIM_NAME = "Authorization"; // const AUTH_USER_ID = "token-id"; const serverApiUrl = process.env.NEXT_PUBLIC_CS_SERVER_API_URL; const GEOSERVER_URL = process.env.NEXT_PUBLIC_GEOSERVER_URL || ""; const LX_GEOSERVER_URL = process.env.NEXT_PUBLIC_LX_GEOSERVER_URL || ""; const GATEWAY_URL = process.env.NEXT_PUBLIC_GATEWAY_URL || ""; // const SEWERAGE_API_URL = process.env.NEXT_PUBLIC_SEWERAGE_API_URL || ""; const BUSINESS_JUSO_API_URL = process.env.NEXT_PUBLIC_FINDADRESS_URL || ""; const AdressKey = process.env.NEXT_PRIVATE_WR_ADDRESS_KEY || ""; /** 공통: URLSearchParams의 값을 decodeURIComponent 적용하여 querystring 생성 */ function toDecodedQueryString(params: URLSearchParams): string { const entries: string[][] = Array.from(params.entries()).map(([k, v]) => { try { return [k, decodeURIComponent(v)] as string[]; } catch { return [k, v] as string[]; } }); return new URLSearchParams(entries).toString(); } export function middleware(req: NextRequest) { const pathname = req.nextUrl.pathname; console.log(`[Middleware] Request URL: ${req.url}`); const searchParams = req.nextUrl.searchParams; if (pathname.startsWith("/server")) { const decodedParams = Array.from(searchParams.entries()) .map(([key, value]) => `${key}=${decodeURIComponent(value)}`) .join("&"); const newUrl = `${serverApiUrl}${pathname.replace("/server", "")}${decodedParams ? `?${decodedParams}` : "" }`; return NextResponse.rewrite(new URL(newUrl)); } if (pathname === "/") { console.log(" maim 화면이동 "); return NextResponse.redirect(new URL("/main", req.url)); } 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); console.log(`[Middleware] Rewriting from '/attach-service' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl), { headers: requestHeaders, }); } if (pathname.startsWith("/user-service") || pathname.startsWith("/cms-service")) { let queryParams = searchParams.toString(); const requestHeaders = new Headers(req.headers); const newUrl = `${serverApiUrl}${pathname}${queryParams ? `?${queryParams}` : ""}`; console.log(`[Middleware] Rewriting from '/user-service' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl), { headers: requestHeaders, }); } if (pathname.startsWith("/geoserver")) { let queryParams = searchParams.toString(); const typename = searchParams.get("TYPENAME"); const startYmd = searchParams.get("START_YMD") ?? null; const endYmd = searchParams.get("END_YMD") ?? null; console.log("typename ", typename); let newUrl = `${GEOSERVER_URL}${pathname.replace("/geoserver", "")}${queryParams ? `?${queryParams}` : ""}`; if ( typename == "hasudo:tb_il_swage_prcs_fclt" || //하수처리시설 typename == "hasudo:tb_ms_swage_prcs_zone" || //하수처리구역 typename == "hasudo:tb_ms_sbmrg_dngr_map" || //침수위험지도 typename == "hasudo:tb_ms_flood_dngr_map" //도시침수지도 ) { newUrl = `${GEOSERVER_URL}${pathname.replace("/geoserver", "")}${queryParams ? `?${queryParams}` : ""}`; } else if ( typename == "hasudo:tb_ms_swage_ppl" || //관로 typename == "hasudo:tb_ms_swage_mhl" || //맨홀 typename == "hasudo:tb_ms_swage_pmstn" || //펌프장 typename == "hasudo:tb_ms_stofcbr" //우수토실 ) { newUrl = `${LX_GEOSERVER_URL}${pathname.replace("/geoserver", "")}${queryParams ? `?${queryParams}` : ""}`; } else if ( typename == "hasudo:om_dt_untrt_swg_rain" || //중점우수토실 typename == "hasudo:om_wqlt_station" || //관측소 typename == "hasudo:tb_wr_bdst_ruse_fclt" || //재이용시설 typename == "hasudo:tb_wr_rnwtr_utztn_fclt" || //빗물이용시설 typename == "hasudo:tb_wr_rswtr_fclt" || //중수도시설 typename == "hasudo:tb_wr_htdfl_fclt" || //온배수시설 typename == "hasudo:tb_wr_ruse_fclt_dmd_co" || //수요처 typename == "hasudo:tb_wr_tmpr_dmd_co" //잠재수요처 ) { const url = req.nextUrl.clone(); url.searchParams.set("TYPENAME", typename.replace("hasudo:", "")); if (startYmd) url.searchParams.set("startYmd", startYmd); if (endYmd) url.searchParams.set("endYmd", endYmd); queryParams = url.searchParams.toString(); newUrl = `${GATEWAY_URL}/sewerage-service/api/v1/map/geometry${queryParams ? `?${queryParams}` : ""}`; } console.log(`[Middleware] Rewriting from '/geoserver' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } if (pathname.startsWith("/reuse-service")) { const queryParams = searchParams.toString(); const newUrl = `${GATEWAY_URL}${pathname}${queryParams ? `?${queryParams}` : ""}`; console.log(`[Middleware] Rewriting from '/reuse-service' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } else if (pathname.startsWith("/operation-service")) { const queryParams = searchParams.toString(); const newUrl = `${GATEWAY_URL}${pathname}${queryParams ? `?${queryParams}` : ""}`; console.log(`[Middleware] Rewriting from '/operation-service' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } else if (pathname.startsWith("/sewerage-service")) { const queryParams = searchParams.toString(); const newUrl = `${GATEWAY_URL}${pathname}${queryParams ? `?${queryParams}` : ""}`; console.log(`[Middleware] Rewriting from '/sewerage-service' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } if (pathname.startsWith("/ngii-server")) { const decodedQuery = toDecodedQueryString(searchParams); const newUrl = `${process.env.NEXT_PUBLIC_NGII_URL}${pathname.replace("/ngii-server", "")}${decodedQuery ? `?${decodedQuery}` : ""}`; console.log(`[Middleware] Rewriting from '${pathname}' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } if (pathname.startsWith("/bussiness-juso-service")) { console.log("AdressKey ", AdressKey); const params = new URLSearchParams({ currentPage: "1", // 원하는 페이지 countPerPage: "5", // 페이지당 결과 수 keyword: searchParams.get("keyword") || "", // 검색어(주소) confmKey: AdressKey, // 인증키 인터넷용 패쇄망용은 따로 발급 필요 즉 middleware에서 치환해서 사용해야할듯 resultType: "json", // 결과 형식 기본은 xml }); const newUrl = `${BUSINESS_JUSO_API_URL}?${params.toString()}`; console.log(`[Middleware] Request to business.juso.go.kr`); console.log(req.url); console.log(`[Middleware] Rewriting from '/business.juso.go.kr' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } return NextResponse.next(); } export const config = { matcher: [ "/server/:path*", "/user-service/:path*", "/cms-service/:path*", "/attach-service/:path*", "/geoserver/:path*", "/operation-service/:path*", "/reuse-service/:path*", "/sewerage-service/:path*", "/bussiness-juso-service/:path*", "/ngii-server/:path*", "/", ], };