refactor: use relative URLs and centralize API endpoints (closes #431) #432
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
After migrating to
https://balancerproject.org/, the frontend was still calling the old API endpoint (balancer.live.k8s.phl.io), causing:Solution
This MR refactors the frontend to use relative URLs instead of environment-specific base URLs, ensuring the same Docker image works in both production and sandbox environments.
Key Changes
Django URL Routing
/api/prefix/admin/outside the API prefixFrontend API Configuration
baseURLfromVITE_API_BASE_URLto empty string''(relative URLs)endpoints.tsfor maintainable API URL managementUpdated All Frontend Components
VITE_API_BASE_URLandREACT_APP_API_BASE_URLdependencies/auth/...→/api/auth/.../v1/api/...→/api/v1/api/.../chatgpt/...→/api/chatgpt/.../ai_settings/...→/api/ai_settings/...Files Changed
Backend:
server/balancer_backend/urls.py- Wrap all APIs under/api/prefixFrontend:
frontend/src/api/apiClient.ts- Use relative URLs and centralized endpointsfrontend/src/api/endpoints.ts- New file - Centralized endpoint configurationfrontend/src/services/actions/auth.tsx- Use centralized auth endpointsfrontend/src/pages/Settings/SettingsManager.tsx- Use centralized endpointsfrontend/src/pages/Files/FileRow.tsx- Use centralized endpointsfrontend/src/pages/DrugSummary/PDFViewer.tsx- Use centralized endpointsDocumentation:
frontend/API_ENDPOINTS_REFACTORING.md- Guide for maintaining endpointsBenefits
✅ Same Docker image works in both production (
balancerproject.org) and sandbox (balancer.sandbox.k8s.phl.io)✅ No environment variables needed for API base URL
✅ Automatic domain detection via relative URLs
✅ Maintainable - All endpoints defined in one place
✅ Type-safe - TypeScript ensures correct endpoint usage
Testing
After deployment, verify:
https://balancerproject.org/loginloads correctlyhttps://balancerproject.org/adminportalloads correctly/api/auth/login/, etc.)Related
Closes #431