Skip to content

Commit bbe3c46

Browse files
dadachiclaude
andcommitted
Require HOST explicitly instead of auto-detecting the Wi-Fi IP
Auto-detection (ipconfig getifaddr en0 in Procfile.dev, Socket on the mailer side) worked against the invariant: the mobile apps hardcode the IP in the Xcode scheme and ~/.gradle/gradle.properties, so if Rails silently picked a different one (DHCP rotation, Tailscale or VPN interface shadowing en0), the three sides diverged without failing. Require HOST in .env via \${HOST:?...} so `bin/dev` errors immediately when unset, matching the explicit-IP posture already needed on mobile. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 47efcc6 commit bbe3c46

5 files changed

Lines changed: 16 additions & 13 deletions

File tree

.env.sample

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
# Copy to .env and edit. Loaded by Foreman when running `bin/dev`.
1+
# Copy to .env and set HOST to your current Wi-Fi IP, then Foreman
2+
# auto-loads this when running `bin/dev`.
23
#
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.
4+
# Rails, the iOS app, and the Android app must all agree on one
5+
# reachable address — your host's current Wi-Fi IP. On macOS:
6+
#
7+
# ipconfig getifaddr en0
8+
#
9+
# Never use 127.0.0.1, localhost, or 0.0.0.0. When your Wi-Fi IP
10+
# changes, update HOST here and the matching values in the mobile
11+
# apps (Xcode scheme NATEMPLATE_API_DOMAIN and Android
12+
# ~/.gradle/gradle.properties NATEMPLATE_API_DOMAIN).
813

14+
HOST=192.168.1.6
915
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 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`.
99+
- `HOST` (Wi-Fi IP) and `PORT` are required in `.env`; `Procfile.dev` uses `${HOST:?...}` so Rails fails loudly if unset, and `development.rb` uses `ENV.fetch("HOST")` for `action_mailer.default_url_options`. Must match `NATEMPLATE_API_DOMAIN` in the iOS scheme and Android `gradle.properties`. Never `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:-$(ipconfig getifaddr en0)}
1+
web: bin/rails server -p ${PORT:-3000} -b ${HOST:?HOST is required - set it in .env to your current Wi-Fi IP}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bin/setup
8585

8686
## Running NativeAppTemplate API on your Wi-Fi
8787

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.
88+
Copy `.env.sample` to `.env` and set `HOST` to your current Wi-Fi IP. On macOS: `ipconfig getifaddr en0`. `bin/dev` binds Rails to that address so the dev server is reachable from both the host browser and from any phone on the same network at `http://<wifi-ip>:3000`. When your Wi-Fi IP changes, update `HOST` here and the matching `NATEMPLATE_API_DOMAIN` in the mobile apps (Xcode scheme for iOS, `~/.gradle/gradle.properties` for Android) — Rails fails loudly if `HOST` is unset, which keeps the three sides honest. Never use `127.0.0.1`, `localhost`, or `0.0.0.0`.
8989

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

config/environments/development.rb

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

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

4039
config.action_mailer.perform_caching = false
4140

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}
41+
config.action_mailer.default_url_options = {host: ENV.fetch("HOST"), port: ENV.fetch("PORT", 3000).to_i}
4442

4543
# Print deprecation notices to the Rails logger.
4644
config.active_support.deprecation = :log

0 commit comments

Comments
 (0)