Skip to content

Commit

Permalink
Merge pull request #28 from DeBuXer/reload_env
Browse files Browse the repository at this point in the history
Reload .env file on change without restarting the application
  • Loading branch information
willnode authored Sep 11, 2024
2 parents 639eb79 + e48d212 commit 5f85cc7
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
import {plainServer, secureServer} from "./index.js";
import { clearConfig } from "./src/util.js";
import fs from "fs";
import { watch } from "chokidar";
import dotenv from "dotenv";

// Function to reload the .env variables
function reloadEnv() {
if (fs.existsSync('.env')) {
const envConfig = dotenv.parse(fs.readFileSync('.env'));
for (const k in envConfig) {
process.env[k] = envConfig[k];
}
console.log('Environment variables reloaded.');
} else {
console.warn('.env file does not exist.');
}
}

// Watch the .env file for changes
watch('.env').on('change', () => {
console.log('.env file changed, reloading...');
clearConfig();
reloadEnv();
});

// Initial load
reloadEnv();


const port80 = parseInt(process.env.HTTP_PORT || "8080");
const port443 = parseInt(process.env.HTTPS_PORT || "8443");
Expand Down
200 changes: 200 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"dependencies": {
"async-lock": "^1.4.1",
"better-sqlite3": "^11.1.2",
"chokidar": "^3.5.3",
"dotenv": "^16.0.3",
"jose": "^5.6.3",
"lru-cache": "^11.0.0",
"node-forge": "^1.3.1",
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ function csvToMap(str) {
}, initMap())
}

/**
* @return {void}
*/
export function clearConfig() {
whitelistMap = null;
blacklistMap = null;
useLocalDNS = null;
blacklistRedirectUrl = null;
}

/**
* @param {Record<string, string>} [mockEnv]
*/
Expand Down

0 comments on commit 5f85cc7

Please sign in to comment.