/** * Utility to determine the API base URL for the application. * Using relative paths in production ensures the application works * seamlessly regardless of the Hugging Face Space URL and eliminates CORS issues. */ const getApiBaseUrl = () => { // In production, default to relative paths if (process.env.NODE_ENV === "production") { // If an explicit API URL is provided via env, use it. // Otherwise, return empty string for relative paths. return process.env.REACT_APP_API_URL || ""; } // In development, default to the local backend port return process.env.REACT_APP_API_URL || "http://localhost:7860"; }; /** * Returns the full API URL by appending the /api prefix */ export const getFullApiUrl = () => { const baseUrl = getApiBaseUrl(); // If baseUrl is empty, it means we are using relative paths if (baseUrl === "") return "/api"; // Ensure we don't end up with //api if baseUrl ends with / return `${baseUrl.replace(/\/$/, "")}/api`; }; export default getApiBaseUrl;