pre-dump ENV to protect settings on reboot

This commit is contained in:
timothycarambat 2023-08-15 16:00:27 -07:00
parent cf0b24af02
commit 71baeb92a3
2 changed files with 39 additions and 1 deletions

View File

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

View File

@ -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,
};