feat: add HOST environment variable for configurable bind address#360
feat: add HOST environment variable for configurable bind address#360mjfork wants to merge 2 commits intositeboon:mainfrom
Conversation
Allow users to specify which host/IP address the servers should bind to via the HOST environment variable. Defaults to 0.0.0.0 (all interfaces) to maintain backward compatibility. This enables users to restrict the server to localhost only (127.0.0.1) for security purposes or bind to a specific network interface. Co-Authored-By: Claude Opus 4.5 <[email protected]>
WalkthroughAdds a configurable Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
vite.config.js (1)
11-22:⚠️ Potential issue | 🟠 MajorProxy targets ignore HOST and can break non-local binds.
IfHOSTis set to a specific interface (e.g.,192.168.1.100), the backend binds only there, but the Vite proxy still targetslocalhost, which will fail. Use a derivedproxyHostthat falls back tolocalhostonly whenHOST=0.0.0.0.🐛 Proposed fix
export default defineConfig(({ command, mode }) => { // Load env file based on `mode` in the current working directory. const env = loadEnv(mode, process.cwd(), '') + const host = env.HOST || '0.0.0.0' + const proxyHost = host === '0.0.0.0' ? 'localhost' : host return { plugins: [react()], server: { - host: env.HOST || '0.0.0.0', + host, port: parseInt(env.VITE_PORT) || 5173, proxy: { - '/api': `http://localhost:${env.PORT || 3001}`, + '/api': `http://${proxyHost}:${env.PORT || 3001}`, '/ws': { - target: `ws://localhost:${env.PORT || 3001}`, + target: `ws://${proxyHost}:${env.PORT || 3001}`, ws: true }, '/shell': { - target: `ws://localhost:${env.PORT || 3001}`, + target: `ws://${proxyHost}:${env.PORT || 3001}`, ws: true } } },
🧹 Nitpick comments (1)
server/index.js (1)
1801-1829: Consider a user-friendly URL when HOST is 0.0.0.0.
Line 1829 will printhttp://0.0.0.0:PORT, which isn’t a connectable URL for most users. A display host (e.g.,localhostwhen bound to all interfaces) avoids confusion.♻️ Suggested tweak
-const HOST = process.env.HOST || '0.0.0.0'; +const HOST = process.env.HOST || '0.0.0.0'; +const DISPLAY_HOST = HOST === '0.0.0.0' ? 'localhost' : HOST; ... - console.log(`${c.info('[INFO]')} Server URL: ${c.bright('http://' + HOST + ':' + PORT)}`); + console.log(`${c.info('[INFO]')} Server URL: ${c.bright('http://' + DISPLAY_HOST + ':' + PORT)}`);
When HOST is set to a specific interface (e.g., 192.168.1.100), the Vite proxy needs to connect to that same host, not localhost. This fix: - Adds proxyHost logic that uses localhost when HOST=0.0.0.0, otherwise uses the specific HOST value for proxy targets - Adds DISPLAY_HOST to show a user-friendly URL in console output (0.0.0.0 isn't a connectable address) Addresses CodeRabbit review feedback. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Summary
HOSTenvironment variable to configure which host/IP the servers bind to0.0.0.0(all interfaces) for backward compatibilityFixes #359
Changes
.env.example: AddedHOSTenvironment variable with documentationserver/index.js: UseHOSTenv var instead of hardcoded'0.0.0.0'vite.config.js: Addedhostoption to Vite server configTest plan
HOST=127.0.0.1and verify server only listens on localhostHOSTunset and verify server listens on all interfaces (default behavior)🤖 Generated with Claude Code
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.