From 48cb0be79588117a6f02b7e3520684578c9d453a Mon Sep 17 00:00:00 2001 From: saurabhhhcodes <170401851+saurabhhhcodes@users.noreply.github.com> Date: Wed, 10 Jun 2026 16:35:16 +0530 Subject: [PATCH] fix: configure frontend api base url --- README.md | 2 ++ frontend/.env.example | 1 + frontend/src/services/api.ts | 14 +++++++++++--- frontend/src/vite-env.d.ts | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 frontend/.env.example create mode 100644 frontend/src/vite-env.d.ts diff --git a/README.md b/README.md index eed906ebf..5a07d6a38 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ npm install npm run dev ``` +For hosted frontend deployments, set `VITE_API_BASE_URL` to the backend API origin, for example `http://localhost:8000/api/v1` locally or your deployed backend URL in production. + ### Option 3 — Ollama (free, no API key) ```bash diff --git a/frontend/.env.example b/frontend/.env.example new file mode 100644 index 000000000..a8a8970b4 --- /dev/null +++ b/frontend/.env.example @@ -0,0 +1 @@ +VITE_API_BASE_URL=http://localhost:8000/api/v1 diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index c77f723cd..5620ff47e 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -1,8 +1,16 @@ import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios' import { useAuthStore } from '../stores/authStore' +const configuredApiBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim() +const API_BASE_URL = configuredApiBaseUrl ? configuredApiBaseUrl.replace(/\/$/, '') : '/api/v1' + +function buildApiUrl(path: string): string { + const normalizedPath = path.startsWith('/') ? path : `/${path}` + return `${API_BASE_URL}${normalizedPath}` +} + const api = axios.create({ - baseURL: '/api/v1', + baseURL: API_BASE_URL, headers: { 'Content-Type': 'application/json', }, @@ -341,7 +349,7 @@ export const ragApi = { signal?: AbortSignal, ): Promise => { const token = useAuthStore.getState().token - const resp = await fetch('/api/v1/rag/query/stream', { + const resp = await fetch(buildApiUrl('/rag/query/stream'), { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -493,4 +501,4 @@ export const guardHistoryApi = { }, } -export default api \ No newline at end of file +export default api diff --git a/frontend/src/vite-env.d.ts b/frontend/src/vite-env.d.ts new file mode 100644 index 000000000..11f02fe2a --- /dev/null +++ b/frontend/src/vite-env.d.ts @@ -0,0 +1 @@ +///