diff --git a/backend/classes/env.js b/backend/classes/env.js new file mode 100644 index 0000000..6491b6d --- /dev/null +++ b/backend/classes/env.js @@ -0,0 +1,29 @@ +const fs = require("fs"); +const path = require("path"); + +async function writeEnvVariables() { + // Fetch environment variables that start with JS_ + const envVariables = Object.keys(process.env).reduce((acc, key) => { + if (key.startsWith("JS_")) { + acc[key] = process.env[key]; + } + return acc; + }, {}); + + // Convert the environment variables to a JavaScript object string + const envContent = `window.env = ${JSON.stringify(envVariables, null, 2)};`; + + // Define the output file path + const outputPath = path.join(__dirname, "..", "..", "dist", "env.js"); + + // Write the environment variables to the file + fs.writeFile(outputPath, envContent, "utf8", (err) => { + if (err) { + console.error("Error writing env.js file:", err); + } else { + console.log("env.js file has been saved successfully."); + } + }); +} + +module.exports = writeEnvVariables; diff --git a/backend/server.js b/backend/server.js index 01fd27a..d78fbc3 100644 --- a/backend/server.js +++ b/backend/server.js @@ -31,6 +31,7 @@ const tasks = require("./tasks/tasks"); // websocket const { setupWebSocketServer } = require("./ws"); +const writeEnvVariables = require("./classes/env"); const app = express(); const db = knex(knexConfig.development); @@ -97,10 +98,12 @@ app.use("/utils", authenticate, utilsRouter, () => { app.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); // for deployment of static page -const root = path.join(__dirname, "..", "dist"); -app.use(express.static(root)); -app.get("*", (req, res) => { - res.sendFile(path.join(__dirname, "..", "dist", "index.html")); +writeEnvVariables().then(() => { + const root = path.join(__dirname, "..", "dist"); + app.use(express.static(root)); + app.get("*", (req, res) => { + res.sendFile(path.join(__dirname, "..", "dist", "index.html")); + }); }); // JWT middleware diff --git a/index.html b/index.html index fb0fe71..c78025c 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@ +