From d6446c9c029feaf1d5afe168a2c5606cce5c31bc Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Fri, 20 Aug 2021 02:20:28 +0300 Subject: [PATCH] Add `.env.example` to React app --- .gitignore | 3 +++ Dockerfile | 1 + ui/.env.example | 2 ++ ui/src/helpers/api.js | 10 ++++------ 4 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 ui/.env.example diff --git a/.gitignore b/.gitignore index b2b8be164..59674fecc 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ build # Ignore the scripts that are created for development *dev.* + +# Environment variables +.env diff --git a/Dockerfile b/Dockerfile index 6ed1d15d2..75f2d2338 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ FROM node:14-slim AS site-build WORKDIR /app/ui-build COPY ui . +RUN rm .env RUN npm i RUN npm run build diff --git a/ui/.env.example b/ui/.env.example new file mode 100644 index 000000000..cebc16952 --- /dev/null +++ b/ui/.env.example @@ -0,0 +1,2 @@ +REACT_APP_OVERRIDE_WS_URL="ws://localhost:8899/ws" +REACT_APP_OVERRIDE_API_URL="http://localhost:8899/api/" diff --git a/ui/src/helpers/api.js b/ui/src/helpers/api.js index ca0044123..6b8626cac 100644 --- a/ui/src/helpers/api.js +++ b/ui/src/helpers/api.js @@ -2,17 +2,15 @@ import * as axios from "axios"; const mizuAPIPathPrefix = "/mizu"; -// When working locally (with npm run start) change to: -// export const MizuWebsocketURL = `ws://localhost:8899${mizuAPIPathPrefix}/ws`; -export const MizuWebsocketURL = `ws://${window.location.host}${mizuAPIPathPrefix}/ws`; +// When working locally cp `cp .env.example .env` +export const MizuWebsocketURL = process.env.REACT_APP_OVERRIDE_WS_URL ? process.env.REACT_APP_OVERRIDE_WS_URL : `ws://${window.location.host}${mizuAPIPathPrefix}/ws`; export default class Api { constructor() { - // When working locally (with npm run start) change to: - // const apiURL = `http://localhost:8899/${mizuAPIPathPrefix}/api/`; - const apiURL = `${window.location.origin}${mizuAPIPathPrefix}/api/`; + // When working locally cp `cp .env.example .env` + const apiURL = process.env.REACT_APP_OVERRIDE_API_URL ? process.env.REACT_APP_OVERRIDE_API_URL : `${window.location.origin}${mizuAPIPathPrefix}/api/`; this.client = axios.create({ baseURL: apiURL,