From b4e01641a040af24720511d9917c420667e19edb Mon Sep 17 00:00:00 2001 From: hyeonsu Date: Thu, 4 Sep 2025 13:56:26 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A1=B0=EA=B1=B4=EB=B6=80=20API=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/lib/api/client.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/frontend/lib/api/client.ts b/frontend/lib/api/client.ts index a141be3f..c73c53eb 100644 --- a/frontend/lib/api/client.ts +++ b/frontend/lib/api/client.ts @@ -1,7 +1,29 @@ import axios, { AxiosResponse, AxiosError } from "axios"; -// API 기본 URL 설정 - 모든 접근에서 내부 IP 사용 (Option B) -export const API_BASE_URL = "http://192.168.0.70:8080/api"; +// API 기본 URL 동적 설정 - 접속 위치에 따라 최적 경로 선택 +const getApiBaseUrl = (): string => { + if (typeof window !== "undefined") { + const currentHost = window.location.hostname; + + // 외부 IP로 접근한 경우 - 외부 IP로 API 호출 + if (currentHost === "39.117.244.52") { + return "http://39.117.244.52:8080/api"; + } + // 내부 IP로 접근한 경우 - 내부 IP로 API 호출 + else if (currentHost === "192.168.0.70") { + return "http://192.168.0.70:8080/api"; + } + // localhost로 접근한 경우 - localhost로 API 호출 + else if (currentHost === "localhost" || currentHost === "127.0.0.1") { + return "http://localhost:8080/api"; + } + } + + // 서버 사이드 렌더링이나 기본값 - 외부 IP 사용 + return process.env.NEXT_PUBLIC_API_URL || "http://39.117.244.52:8080/api"; +}; + +export const API_BASE_URL = getApiBaseUrl(); // JWT 토큰 관리 유틸리티 const TokenManager = { @@ -43,6 +65,7 @@ apiClient.interceptors.request.use( tokenStart: token ? token.substring(0, 30) + "..." : "없음", url: config.url, method: config.method, + baseURL: config.baseURL, }); if (token && !TokenManager.isTokenExpired(token)) { @@ -92,7 +115,9 @@ apiClient.interceptors.request.use( } } - console.log("📡 API 요청:", config.method?.toUpperCase(), config.url, config.params, config.data); + // TypeScript 안전성을 위한 null 체크 + const fullUrl = `${config.baseURL || ""}${config.url || ""}`; + console.log("📡 API 요청:", config.method?.toUpperCase(), fullUrl, config.params, config.data); console.log("📋 요청 헤더:", config.headers); return config; },