[PR #463] [MERGED] feat(env): hot-reload .env changes without restarting the server #4629

Closed
opened 2026-10-22 04:09:18 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/463
Author: @LukaTheHero
Created: 5/6/2026
Status: ✅ Merged
Merged: 5/6/2026
Merged by: @LukaTheHero

Base: main ← Head: feat/env-hot-reload


📝 Commits (1)

  • e62dcdd feat(env): hot-reload .env changes without restarting the server

📊 Changes

5 files changed (+243 additions, -5 deletions)

View changed files

📝 docs/CONFIGURATION.md (+2 -0)
📝 docs/REMOTE_ACCESS.md (+9 -3)
➕ packages/server/src/config/env-watcher.ts (+156 -0)
📝 packages/server/src/config/runtime-config.ts (+73 -2)
📝 packages/server/src/index.ts (+3 -0)

📄 Description

Summary

  • Watches the monorepo-root .env and propagates added / updated / removed keys to process.env on save.
  • Most security middleware already reads via runtime-config.ts getters that re-evaluate process.env per request, so changes to BASIC_AUTH_USER / BASIC_AUTH_PASS / BASIC_AUTH_REALM, IP_ALLOWLIST / IP_ALLOWLIST_ENABLED, ADMIN_SECRET, CSRF_TRUSTED_ORIGINS, TRUSTED_PRIVATE_NETWORKS, and the ALLOW_UNAUTHENTICATED_* / *_LOCAL_URLS_ENABLED flags now take effect on the next request — no restart required.
  • Boot-bound variables (PORT, HOST, SSL_CERT/SSL_KEY, CORS_ORIGINS, storage paths, ENCRYPTION_KEY, TZ, AUTO_OPEN_*) still need a restart; the watcher logs a warning identifying which one changed so users aren't left wondering why nothing happened.

Why

Users adding access-control settings (Basic Auth, IP allowlist) hit a recurring papercut: edit .env, forget to restart, conclude the feature is broken, file an issue. The middleware was already architected for per-request reads — the only blocker was a one-time dotenv.config() guard at module load. This PR adds a polling watcher (2 s fs.watchFile) and a reloadRuntimeEnv() helper that re-parses the file and applies the diff to process.env (including key removal, so unsetting a value works). LOG_LEVEL changes also flow through to the live pino logger.

What changed

  • packages/server/src/config/runtime-config.ts — loadRuntimeEnv() now records the keys it set; new reloadRuntimeEnv() returns an { added, updated, removed, unchanged } diff after re-parsing.
  • packages/server/src/config/env-watcher.ts (new) — polling watcher, debounces by mtime + size, masks sensitive values (BASIC_AUTH_PASS, ADMIN_SECRET, ENCRYPTION_KEY, GIPHY_API_KEY) when logging, warns on restart-required keys, applies LOG_LEVEL live.
  • packages/server/src/index.ts — starts the watcher after buildApp(), stops it on graceful shutdown.
  • docs/REMOTE_ACCESS.md + docs/CONFIGURATION.md — new "When a restart is required" section + tweaked the "restart Marinara" wording in the access-control walkthrough.

Notes

  • No linked issue. Per CONTRIBUTING.md, one should be opened describing the user-facing problem (Basic Auth / IP allowlist feedback loop friction) before this is merged.
  • Polling at 2 s on a tiny text file is negligible CPU; chose fs.watchFile over fs.watch for cross-platform reliability (editors that write via temp-file + atomic rename break fs.watch on Windows).

Summary by CodeRabbit

Release Notes

  • New Features

    • Environment variables now support hot-reload: most configuration changes take effect within seconds without requiring a server restart via a new environment watcher.
    • A subset of low-level environment variables still require a full restart to take effect.
  • Documentation

    • Updated configuration and remote access guides to clarify which environment variables support hot-reload and which require a restart.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/463 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 5/6/2026 **Status:** ✅ Merged **Merged:** 5/6/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `feat/env-hot-reload` --- ### 📝 Commits (1) - [`e62dcdd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e62dcdd8673885d5eb5c67393de76c2fd83c7ab1) feat(env): hot-reload .env changes without restarting the server ### 📊 Changes **5 files changed** (+243 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `docs/CONFIGURATION.md` (+2 -0) 📝 `docs/REMOTE_ACCESS.md` (+9 -3) ➕ `packages/server/src/config/env-watcher.ts` (+156 -0) 📝 `packages/server/src/config/runtime-config.ts` (+73 -2) 📝 `packages/server/src/index.ts` (+3 -0) </details> ### 📄 Description ## Summary - Watches the monorepo-root `.env` and propagates added / updated / removed keys to `process.env` on save. - Most security middleware already reads via `runtime-config.ts` getters that re-evaluate `process.env` per request, so changes to `BASIC_AUTH_USER` / `BASIC_AUTH_PASS` / `BASIC_AUTH_REALM`, `IP_ALLOWLIST` / `IP_ALLOWLIST_ENABLED`, `ADMIN_SECRET`, `CSRF_TRUSTED_ORIGINS`, `TRUSTED_PRIVATE_NETWORKS`, and the `ALLOW_UNAUTHENTICATED_*` / `*_LOCAL_URLS_ENABLED` flags now take effect on the next request — no restart required. - Boot-bound variables (`PORT`, `HOST`, `SSL_CERT`/`SSL_KEY`, `CORS_ORIGINS`, storage paths, `ENCRYPTION_KEY`, `TZ`, `AUTO_OPEN_*`) still need a restart; the watcher logs a warning identifying which one changed so users aren't left wondering why nothing happened. ## Why Users adding access-control settings (Basic Auth, IP allowlist) hit a recurring papercut: edit `.env`, forget to restart, conclude the feature is broken, file an issue. The middleware was already architected for per-request reads — the only blocker was a one-time `dotenv.config()` guard at module load. This PR adds a polling watcher (2 s `fs.watchFile`) and a `reloadRuntimeEnv()` helper that re-parses the file and applies the diff to `process.env` (including key removal, so unsetting a value works). `LOG_LEVEL` changes also flow through to the live pino logger. ## What changed - `packages/server/src/config/runtime-config.ts` — `loadRuntimeEnv()` now records the keys it set; new `reloadRuntimeEnv()` returns an `{ added, updated, removed, unchanged }` diff after re-parsing. - `packages/server/src/config/env-watcher.ts` (new) — polling watcher, debounces by mtime + size, masks sensitive values (`BASIC_AUTH_PASS`, `ADMIN_SECRET`, `ENCRYPTION_KEY`, `GIPHY_API_KEY`) when logging, warns on restart-required keys, applies `LOG_LEVEL` live. - `packages/server/src/index.ts` — starts the watcher after `buildApp()`, stops it on graceful shutdown. - `docs/REMOTE_ACCESS.md` + `docs/CONFIGURATION.md` — new "When a restart is required" section + tweaked the "restart Marinara" wording in the access-control walkthrough. ## Notes - No linked issue. Per `CONTRIBUTING.md`, one should be opened describing the user-facing problem (Basic Auth / IP allowlist feedback loop friction) before this is merged. - Polling at 2 s on a tiny text file is negligible CPU; chose `fs.watchFile` over `fs.watch` for cross-platform reliability (editors that write via temp-file + atomic rename break `fs.watch` on Windows). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **New Features** * Environment variables now support hot-reload: most configuration changes take effect within seconds without requiring a server restart via a new environment watcher. * A subset of low-level environment variables still require a full restart to take effect. * **Documentation** * Updated configuration and remote access guides to clarify which environment variables support hot-reload and which require a restart. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:18 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4629
No description provided.