Skip to content

Conversation

@TineoC
Copy link
Collaborator

@TineoC TineoC commented Jan 6, 2026

Problem

After migrating to https://balancerproject.org/, the frontend was still calling the old API endpoint (balancer.live.k8s.phl.io), causing:

  • API calls to fail due to old domain reference
  • /login page returning error/is inaccessible
  • /adminportal page returning error/is inaccessible
  • Medication recommendations not working

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

  1. Django URL Routing

    • Wrapped all API routes under /api/ prefix
    • Kept /admin/ outside the API prefix
    • SPA catch-all route serves frontend for all other paths
  2. Frontend API Configuration

    • Changed baseURL from VITE_API_BASE_URL to empty string '' (relative URLs)
    • Created centralized endpoints.ts for maintainable API URL management
    • Added helper functions for dynamic endpoints with parameters
  3. Updated All Frontend Components

    • Removed all VITE_API_BASE_URL and REACT_APP_API_BASE_URL dependencies
    • Updated all API calls to use centralized endpoints
    • Updated auth endpoints: /auth/.../api/auth/...
    • Updated API endpoints: /v1/api/.../api/v1/api/...
    • Updated conversation endpoints: /chatgpt/.../api/chatgpt/...
    • Updated settings endpoints: /ai_settings/.../api/ai_settings/...

Files Changed

Backend:

  • server/balancer_backend/urls.py - Wrap all APIs under /api/ prefix

Frontend:

  • frontend/src/api/apiClient.ts - Use relative URLs and centralized endpoints
  • frontend/src/api/endpoints.ts - New file - Centralized endpoint configuration
  • frontend/src/services/actions/auth.tsx - Use centralized auth endpoints
  • frontend/src/pages/Settings/SettingsManager.tsx - Use centralized endpoints
  • frontend/src/pages/Files/FileRow.tsx - Use centralized endpoints
  • frontend/src/pages/DrugSummary/PDFViewer.tsx - Use centralized endpoints
  • Plus 9 additional frontend component files updated

Documentation:

  • frontend/API_ENDPOINTS_REFACTORING.md - Guide for maintaining endpoints

Benefits

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/login loads correctly
  • https://balancerproject.org/adminportal loads correctly
  • ✅ API calls in browser DevTools show relative URLs (/api/auth/login/, etc.)
  • ✅ No 404 errors for API endpoints
  • ✅ Medication recommendations work

Related

Closes #431

TineoC added 2 commits January 6, 2026 18:54
- Introduced a new `endpoints.ts` file to centralize all API endpoint paths for better maintainability and type safety.
- Updated various components and services to utilize the new centralized endpoints, enhancing consistency across the codebase.
- Created a comprehensive `API_ENDPOINTS_REFACTORING.md` guide detailing the refactoring process and usage patterns for the new endpoints.
- Removed hardcoded URLs in favor of the centralized configuration, improving code readability and reducing the risk of errors.
- Update Django URLs to serve all APIs under /api/ prefix
- Change frontend to use relative URLs (empty baseURL) instead of environment-specific domains
- Create centralized endpoints.ts for maintainable API URL management
- Update all frontend components to use centralized endpoints
- Remove all VITE_API_BASE_URL and REACT_APP_API_BASE_URL dependencies
- Add helper functions for dynamic endpoints with parameters

This ensures the same Docker image works in both production and sandbox
environments without requiring environment-specific configuration.

Fixes:
- Frontend calling old domain (balancer.live.k8s.phl.io)
- API calls failing after domain migration
- /login and /adminportal pages not working

Closes CodeForPhilly#431
@TineoC TineoC requested review from sahilds1 and taichan03 and removed request for taichan03 January 6, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant