Skip to content

Commit 47efcc6

Browse files
dadachiclaude
andcommitted
Auto-detect Wi-Fi IP for dev server bind and mailer host
Design invariant: Rails and the mobile apps must agree on one reachable address — the host's current Wi-Fi IP. Never loopback, never localhost, never 0.0.0.0. Procfile.dev now defaults -b to `$(ipconfig getifaddr en0)`, and development.rb picks the first private non-loopback IPv4 via Socket.ip_address_list for action_mailer.default_url_options. HOST in .env overrides when en0 isn't Wi-Fi (wired-primary, Tailscale). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 05f4d70 commit 47efcc6

5 files changed

Lines changed: 14 additions & 8 deletions

File tree

.env.sample

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Copy to .env and edit. Loaded by Foreman when running `bin/dev`.
2+
#
3+
# `bin/dev` auto-detects your Wi-Fi IP (via `ipconfig getifaddr en0` on macOS)
4+
# and binds Rails to it so phones on the same network can reach the dev server.
5+
# Only set HOST if the auto-detected interface is wrong (wired-primary,
6+
# Tailscale, etc.). Never use 127.0.0.1, localhost, or 0.0.0.0 — the
7+
# mobile apps and Rails must agree on the current Wi-Fi IP.
28

3-
# Rails server bind address and port. Use your LAN IP (e.g. 192.168.1.6) for
4-
# mobile-device-on-LAN access; use 127.0.0.1 for loopback-only dev.
5-
HOST=127.0.0.1
69
PORT=3000
10+
# HOST=192.168.1.6

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bin/rails dbconsole # Database console
9696
- Run tests: `bin/rails test` (205 tests, 402 assertions)
9797

9898
### Development Server Configuration
99-
- Server bind and mailer host are env-driven via `HOST`/`PORT` in `.env` (defaults `127.0.0.1:3000`)
99+
- Server bind and mailer host auto-detect the current Wi-Fi IP (`ipconfig getifaddr en0` in `Procfile.dev`; `Socket.ip_address_list` private IPv4 in `development.rb`) so Rails and mobile apps agree on one reachable address; override with `HOST` in `.env` only if `en0` isn't Wi-Fi. Never use `127.0.0.1`, `localhost`, or `0.0.0.0`.
100100
- Mailbin for email testing at `/mailbin`
101101
- Admin interface at `/madmin`
102102
- Tailwind CSS compiled by tailwindcss-rails gem

Procfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: bin/rails server -p ${PORT:-3000} -b ${HOST:-127.0.0.1}
1+
web: bin/rails server -p ${PORT:-3000} -b ${HOST:-$(ipconfig getifaddr en0)}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ Run `bin/setup` to install Ruby and JavaScript dependencies and setup your datab
8383
bin/setup
8484
```
8585

86-
## Running NativeAppTemplate API on localhost
86+
## Running NativeAppTemplate API on your Wi-Fi
8787

88-
Copy `.env.sample` to `.env` and set `HOST` (and optionally `PORT`) to control the server bind address and mailer URL host. Defaults to `127.0.0.1:3000`. For mobile-device-on-LAN access, set `HOST=<your-lan-ip>`. Foreman auto-loads `.env` under `bin/dev`.
88+
`bin/dev` binds Rails to the current Wi-Fi IP (auto-detected via `ipconfig getifaddr en0`), so the dev server is reachable from both the host browser and from any phone on the same network at `http://<wifi-ip>:3000`. To check the IP: `ipconfig getifaddr en0` (or System Settings → Network). If `en0` isn't Wi-Fi on your machine (wired-primary, Thunderbolt networking), copy `.env.sample` to `.env` and set `HOST` to the correct interface's IP. Never use `127.0.0.1`, `localhost`, or `0.0.0.0` — Rails and the mobile apps must agree on the same Wi-Fi IP.
8989

9090
To run your application, you'll use the `bin/dev` command:
9191

config/environments/development.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "active_support/core_ext/integer/time"
2+
require "socket"
23

34
Rails.application.configure do
45
# Settings specified here will take precedence over those in config/application.rb.
@@ -38,7 +39,8 @@
3839

3940
config.action_mailer.perform_caching = false
4041

41-
config.action_mailer.default_url_options = {host: ENV.fetch("HOST", "127.0.0.1"), port: ENV.fetch("PORT", 3000).to_i}
42+
lan_ip = Socket.ip_address_list.find { |a| a.ipv4_private? && !a.ipv4_loopback? }&.ip_address
43+
config.action_mailer.default_url_options = {host: ENV.fetch("HOST") { lan_ip }, port: ENV.fetch("PORT", 3000).to_i}
4244

4345
# Print deprecation notices to the Rails logger.
4446
config.active_support.deprecation = :log

0 commit comments

Comments
 (0)