diff --git a/server/endpoints/system.js b/server/endpoints/system.js index d0b48cc62..5adf14463 100644 --- a/server/endpoints/system.js +++ b/server/endpoints/system.js @@ -10,7 +10,7 @@ const { } = require("../utils/files/documentProcessor"); const { purgeDocument } = require("../utils/files/purgeDocument"); const { getVectorDbClass } = require("../utils/helpers"); -const { updateENV } = require("../utils/helpers/updateENV"); +const { updateENV, dumpENV } = require("../utils/helpers/updateENV"); const { reqBody, makeJWT, @@ -48,6 +48,13 @@ function systemEndpoints(app) { response.sendStatus(200); }); + app.get("/env-dump", async (_, response) => { + if (process.env.NODE_ENV !== "production") + return response.sendStatus(200).end(); + await dumpENV(); + response.sendStatus(200).end(); + }); + app.get("/setup-complete", async (_, response) => { try { const llmProvider = process.env.LLM_PROVIDER || "openai"; diff --git a/server/utils/helpers/updateENV.js b/server/utils/helpers/updateENV.js index d08f25c7a..2abff32dc 100644 --- a/server/utils/helpers/updateENV.js +++ b/server/utils/helpers/updateENV.js @@ -174,6 +174,37 @@ function updateENV(newENVs = {}) { return { newValues, error: error?.length > 0 ? error : false }; } +async function dumpENV() { + const fs = require("fs"); + const path = require("path"); + + const frozenEnvs = {}; + const protectedKeys = [ + ...Object.values(KEY_MAPPING).map((values) => values.envKey), + "CACHE_VECTORS", + "STORAGE_DIR", + "SERVER_PORT", + ]; + + for (const key of protectedKeys) { + const envValue = process.env?.[key] || null; + if (!envValue) continue; + frozenEnvs[key] = process.env?.[key] || null; + } + + var envResult = `# Auto-dump ENV from system call on ${new Date().toTimeString()}\n`; + envResult += Object.entries(frozenEnvs) + .map(([key, value]) => { + return `${key}='${value}'`; + }) + .join("\n"); + + const envPath = path.join(__dirname, "../../.env"); + fs.writeFileSync(envPath, envResult, { encoding: "utf8", flag: "w" }); + return true; +} + module.exports = { + dumpENV, updateENV, };