make API url dynamic

This commit is contained in:
Sean Macfarlane
2020-08-03 12:03:56 -04:00
parent c001ce21ba
commit dc8374300e
6 changed files with 21 additions and 4 deletions

View File

@@ -24,7 +24,11 @@ RUN npm run build
# production environment
FROM nginx:stable-alpine
RUN apk add --no-cache jq
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY docker_entrypoint.sh generate_config_js.sh /
RUN chmod +x docker_entrypoint.sh generate_config_js.sh
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["/docker_entrypoint.sh"]

1
app/config.js Normal file
View File

@@ -0,0 +1 @@
window.REACT_APP_API = undefined;

View File

@@ -9,6 +9,7 @@
<!-- Allow installing the app to the homescreen -->
<meta name="mobile-web-app-capable" content="yes" />
<script type="text/javascript" src="config.js"></script>
</head>
<body>

View File

@@ -18,9 +18,7 @@ import { getItem, setItem, removeItem } from 'utils/localStorage';
import history from 'utils/history';
const API_URI =
process.env.NODE_ENV === 'development'
? 'http://localhost:4000/'
: 'https://wlan-graphql.zone3.lab.connectus.ai/';
process.env.NODE_ENV === 'development' ? 'http://localhost:4000/' : window.REACT_APP_API;
const MOUNT_NODE = document.getElementById('root');
const cache = new InMemoryCache();

3
docker_entrypoint.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/sh -eu
./generate_config_js.sh >/usr/share/nginx/html/config.js
nginx -g "daemon off;"

10
generate_config_js.sh Normal file
View File

@@ -0,0 +1,10 @@
#!/bin/sh -eu
if [ -z "${API:-}" ]; then
API_URL_JSON=undefined
else
API_URL_JSON=$(jq -n --arg api '$API' '$api')
fi
cat <<EOF
window.REACT_APP_API=$API_URL_JSON;
EOF