File size: 1,057 Bytes
024e9c4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /**
* 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;
|