The readme of the react redux realworld.io application at https://github.com/gothinkster/react-redux-realworld-example-app says to edit the src/agent.js
to change the API_ROOT
to point to a different backend api instance. We want to set things up so that API_ROOT
can be defined by an environment variable that is different within the multiple environments (e.g., “staging” and “live”) where we run the production build.
We are running in containers on openshift kubernetes following 12factor.net principles where the code is built once then promoted through environments. We can spin up new environments with a single command so we don’t want to have a switch statement within the code that names each environment and hardcodes the backend API_ROOT
for each environment. Instead, I want to be able to run an existing production build container image in a fresh environment using an environment variable change the API_ROOT
to point to the correct backend API we want to test against.
I have looked at a number of different blogs, stackoverflow answers and the official documentation. The main problem is that typical solutions “bake in” the process.env.API_ROOT
environment variable at build time else have a switch that hardcodes the details of all environments into the code. Neither of which are satisfactory as we want to able to take the latest stable code in an existing container and run it in a new environment using the API running there.
The closest I have got so far is to edit the code to render the process.env.API_ROOT
into a <script>
tag that sets it on a window.API_ROOT
variable. Then check whether that exists else use a default when defining the const for API_ROOT. This feels very invasive and a bit fragile and it is not clear to me where is the best place to render such a script tag in the sample app at https://github.com/gothinkster/react-redux-realworld-example-app