From a38603be2c210e6c52c9232751366b7e660dccc9 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 6 May 2023 09:22:00 +0000 Subject: [PATCH] Merged PR 80: environment variables support environment variables support --- .env | 7 ++----- .env.production | 6 +++--- .gitignore | 1 + dockerfile | 17 +++++++++++++---- public/env.js | 2 ++ public/index.html | 1 + setenv.js | 28 ++++++++++++++++++++++++++++ src/index.js | 3 ++- src/utils/api.js | 9 +++++---- src/utils/env.js | 4 ++++ 10 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 public/env.js create mode 100644 setenv.js create mode 100644 src/utils/env.js diff --git a/.env b/.env index 954b800..61db350 100644 --- a/.env +++ b/.env @@ -1,8 +1,5 @@ -REACT_APP_TUITIO_URL=http://localhost:5063 -# REACT_APP_TUITIO_URL=https://lab.code-rove.com/tuitio - -REACT_APP_NETWORK_RESURRECTOR_API_URL=http://localhost:5064 -#REACT_APP_NETWORK_RESURRECTOR_API_URL=https://lab.code-rove.com/network-resurrector-api +REACT_APP_TUITIO_URL=http://####### +REACT_APP_NETWORK_RESURRECTOR_API_URL=http://####### #600000 milliseconds = 10 minutes REACT_APP_MACHINE_PING_INTERVAL=600000 diff --git a/.env.production b/.env.production index e1901a4..5f0409f 100644 --- a/.env.production +++ b/.env.production @@ -1,6 +1,6 @@ -PUBLIC_URL=/network-resurrector/ -REACT_APP_TUITIO_URL=https://lab.code-rove.com/tuitio -REACT_APP_NETWORK_RESURRECTOR_API_URL=https://lab.code-rove.com/network-resurrector-api +PUBLIC_URL= +REACT_APP_TUITIO_URL=https://####### +REACT_APP_NETWORK_RESURRECTOR_API_URL=https://####### #900000 milliseconds = 15 minutes REACT_APP_MACHINE_PING_INTERVAL=900000 diff --git a/.gitignore b/.gitignore index 80f4a8c..a627bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ # misc .DS_Store .env.local +.env.development .env.development.local .env.test.local .env.production.local diff --git a/dockerfile b/dockerfile index 14523a5..142e57e 100644 --- a/dockerfile +++ b/dockerfile @@ -2,22 +2,31 @@ FROM node:14-slim as builder WORKDIR /app +# global args +ARG APP_SUBFOLDER= + COPY .npmrc .npmrc COPY package*.json ./ RUN npm install RUN rm -f .npmrc COPY . ./ -RUN npm run build + +# set the PUBLIC_URL environment variable +ENV PUBLIC_URL /${APP_SUBFOLDER}/ + +# build the react app +# RUN npm run build +RUN if [ -z "$APP_SUBFOLDER" ]; then npm run build; else PUBLIC_URL=$PUBLIC_URL npm run build; fi + # production environment FROM node:14-slim RUN printf '\n\n- Copy application files\n' -ARG APP_SUBFOLDER=network-resurrector - COPY --from=builder /app/build ./application/${APP_SUBFOLDER} COPY --from=builder /app/build/index.html ./application/ +COPY --from=builder /app/setenv.js ./application/setenv.js #install static server RUN npm install -g serve @@ -35,4 +44,4 @@ WORKDIR / EXPOSE 80 -CMD ["sh", "-c", "serve -s application -p 80"] \ No newline at end of file +CMD ["sh", "-c", "node setenv.js && serve -s application -p 80"] \ No newline at end of file diff --git a/public/env.js b/public/env.js new file mode 100644 index 0000000..433c995 --- /dev/null +++ b/public/env.js @@ -0,0 +1,2 @@ +// In this file will be injected the environment variables that will overwrite the application configurations. +window.env = {}; diff --git a/public/index.html b/public/index.html index 2ebda86..3d2deb4 100644 --- a/public/index.html +++ b/public/index.html @@ -32,6 +32,7 @@ rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" /> + Network resurrector diff --git a/setenv.js b/setenv.js new file mode 100644 index 0000000..abe4d1a --- /dev/null +++ b/setenv.js @@ -0,0 +1,28 @@ +"use strict"; +const fs = require("fs"); +const path = require("path"); + +const prefix = "REACT_APP_"; +const scriptPath = path.join("./application", process.env.PUBLIC_URL, "env.js"); + +function generateScriptContent() { + const prefixRegex = new RegExp(`^${prefix}`); + const env = process.env; + const config = Object.keys(env) + .filter(key => prefixRegex.test(key)) + .reduce((c, key) => Object.assign({}, c, { [key]: env[key] }), {}); + return `window.env=${JSON.stringify(config)};`; +} + +function saveScriptContent(scriptContents) { + fs.writeFile(scriptPath, scriptContents, "utf8", function (err) { + if (err) throw err; + }); +} + +console.log("Setting environment variables..."); +const scriptContent = generateScriptContent(); +saveScriptContent(scriptContent); +console.log( + `Updated ${scriptPath} with ${prefix}* environment variables: ${scriptContent}.` +); diff --git a/src/index.js b/src/index.js index 9d88f5f..f40a159 100644 --- a/src/index.js +++ b/src/index.js @@ -5,10 +5,11 @@ import CssBaseline from "@material-ui/core/CssBaseline"; import AppRouter from "./components/AppRouter"; import { TuitioProvider } from "@flare/tuitio-client-react"; import { ToastProvider } from "./providers"; +import env from "./utils/env"; import "./utils/i18n"; ReactDOM.render( - + Loading...}> diff --git a/src/utils/api.js b/src/utils/api.js index a116652..140db74 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,10 +1,11 @@ import * as axios from "../utils/axios"; import { toast } from "react-toastify"; +import env from "../utils/env"; -const networkRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`; -const systemRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/system`; -const powerActionsRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/resurrector`; -const securityRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/security`; +const networkRoute = `${env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`; +const systemRoute = `${env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/system`; +const powerActionsRoute = `${env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/resurrector`; +const securityRoute = `${env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/security`; const routes = { permissions: `${securityRoute}/permissions`, diff --git a/src/utils/env.js b/src/utils/env.js new file mode 100644 index 0000000..86e54fc --- /dev/null +++ b/src/utils/env.js @@ -0,0 +1,4 @@ +const runtimeEnv = window.env; +const compileEnv = process.env; +const env = { ...compileEnv, ...runtimeEnv }; +export default env;