Merged PR 80: environment variables support

environment variables support
master
Tudor Stanciu 2023-05-06 09:22:00 +00:00
parent 8b1e11c1e3
commit a38603be2c
10 changed files with 61 additions and 17 deletions

7
.env
View File

@ -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

View File

@ -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

1
.gitignore vendored
View File

@ -14,6 +14,7 @@
# misc
.DS_Store
.env.local
.env.development
.env.development.local
.env.test.local
.env.production.local

View File

@ -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"]
CMD ["sh", "-c", "node setenv.js && serve -s application -p 80"]

2
public/env.js Normal file
View File

@ -0,0 +1,2 @@
// In this file will be injected the environment variables that will overwrite the application configurations.
window.env = {};

View File

@ -32,6 +32,7 @@
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
<script src="%PUBLIC_URL%/env.js"></script>
<title>Network resurrector</title>
</head>
<body>

28
setenv.js Normal file
View File

@ -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}.`
);

View File

@ -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(
<TuitioProvider tuitioUrl={process.env.REACT_APP_TUITIO_URL}>
<TuitioProvider tuitioUrl={env.REACT_APP_TUITIO_URL}>
<ThemeProvider>
<CssBaseline />
<Suspense fallback={<div>Loading...</div>}>

View File

@ -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`,

4
src/utils/env.js Normal file
View File

@ -0,0 +1,4 @@
const runtimeEnv = window.env;
const compileEnv = process.env;
const env = { ...compileEnv, ...runtimeEnv };
export default env;