add support for VITE_API_URL environment variable with sensible defaults #116
+16
−3
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.
This update introduces a flexible mechanism to configure the backend API URL used by the frontend, removing the need to hardcode or manually edit the API endpoint within
App.tsx
. The main improvements are as follows:Environment Variable Integration:
The frontend now reads the API URL from an environment variable (
VITE_API_URL
) at build time. This variable can be defined in a.env
file, passed as a build argument in Docker, or set via deployment configuration, making it easy to change the API endpoint for different environments (development, staging, production) without modifying the source code.Build Process Adaptation:
The Dockerfile and build scripts have been updated to accept the
VITE_API_URL
argument and expose it as an environment variable during the frontend build process. This ensures that the correct API URL is embedded into the frontend bundle according to the environment or deployment context.Docker Compose Integration:
The
docker-compose.yaml
file has been updated to support building the entire project directly with Docker Compose. It now passes theVITE_API_URL
environment variable as a build argument, enabling seamless configuration of the API endpoint during the build process without manual intervention.Fallback Logic:
In the application code, the API URL is now resolved using the following logic:
import.meta.env.VITE_API_URL
if defined and non-empty.This allows for robust configuration with minimal manual intervention.
No Need to Edit Source Files:
With this approach, updating the API endpoint only requires changing the environment variable or build argument, not the application source code. This streamlines deployments and reduces the risk of manual errors.
In summary:
The frontend’s API URL is now fully configurable via environment variables and can be set per environment using Docker Compose. The project can be built directly from Docker Compose with the appropriate API endpoint, supporting per-environment deployments and simplifying the process of targeting different backend endpoints—all without requiring changes to
App.tsx
or other source files.