diff --git a/frontend/components/dashboard/widgets/CustomMetricWidget.tsx b/frontend/components/dashboard/widgets/CustomMetricWidget.tsx index 4dfc289e..d6fe8086 100644 --- a/frontend/components/dashboard/widgets/CustomMetricWidget.tsx +++ b/frontend/components/dashboard/widgets/CustomMetricWidget.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react"; import { DashboardElement } from "@/components/admin/dashboard/types"; +import { getApiUrl } from "@/lib/utils/apiUrl"; interface CustomMetricWidgetProps { element?: DashboardElement; @@ -79,7 +80,7 @@ export default function CustomMetricWidget({ element }: CustomMetricWidgetProps) } const token = localStorage.getItem("authToken"); - const response = await fetch("/api/dashboards/execute-query", { + const response = await fetch(getApiUrl("/api/dashboards/execute-query"), { method: "POST", headers: { "Content-Type": "application/json", @@ -121,7 +122,7 @@ export default function CustomMetricWidget({ element }: CustomMetricWidgetProps) } const token = localStorage.getItem("authToken"); - const response = await fetch("/api/dashboards/fetch-external-api", { + const response = await fetch(getApiUrl("/api/dashboards/fetch-external-api"), { method: "POST", headers: { "Content-Type": "application/json", diff --git a/frontend/lib/utils/apiUrl.ts b/frontend/lib/utils/apiUrl.ts new file mode 100644 index 00000000..ea334b86 --- /dev/null +++ b/frontend/lib/utils/apiUrl.ts @@ -0,0 +1,24 @@ +/** + * API URL 유틸리티 + * 프로덕션/개발 환경에 따라 올바른 API URL을 반환 + */ + +export function getApiUrl(endpoint: string): string { + // 클라이언트 사이드에서만 실행 + if (typeof window !== "undefined") { + const hostname = window.location.hostname; + + // 프로덕션: v1.vexplor.com → https://api.vexplor.com + if (hostname === "v1.vexplor.com") { + return `https://api.vexplor.com${endpoint}`; + } + + // 로컬 개발: localhost → http://localhost:8080 + if (hostname === "localhost" || hostname === "127.0.0.1") { + return `http://localhost:8080${endpoint}`; + } + } + + // 기본값: 상대 경로 + return endpoint; +}