Skip to content

Commit bc49187

Browse files
authored
Merge pull request #44 from nativeapptemplate/env-var-refactor
Make dev server bind env-driven via HOST/PORT
2 parents 94b5b4c + 48f62e8 commit bc49187

7 files changed

Lines changed: 33 additions & 5 deletions

File tree

.env.sample

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copy to .env and set HOST to your current Wi-Fi IP. `bin/dev`
2+
# sources this before launching Overmind.
3+
#
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).
13+
14+
HOST=192.168.1.21
15+
PORT=3000

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# Ignore all environment files (except templates).
1111
/.env*
1212
!/.env*.erb
13+
!/.env.sample
1314

1415
# Ignore all logfiles and tempfiles.
1516
/log/*

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 binds to specific IP: `192.168.1.21:3000` (not localhost)
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 -b 192.168.1.21
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: 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-
Replace the IP address `192.168.1.21` with your localhost IP address in `Procfile.dev` and `config/environments/development.rb`.
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

bin/dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/usr/bin/env sh
22

3+
# Load .env so HOST/PORT are inherited by Overmind and the spawned
4+
# processes. Overmind (unlike Foreman) does not auto-load .env.
5+
if [ -f .env ]; then
6+
set -a
7+
. ./.env
8+
set +a
9+
fi
10+
311
# Default to port 3000 if not specified
412
export PORT="${PORT:-3000}"
513

config/environments/development.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838

3939
config.action_mailer.perform_caching = false
4040

41-
config.action_mailer.default_url_options = {host: "192.168.1.21", port: ENV.fetch("PORT", 3000).to_i}
41+
# .env is sourced by bin/dev for the dev server only; direct Rails
42+
# invocations (console, test, setup, CI) don't see it, so default to
43+
# localhost for the mailer so boot succeeds. Procfile.dev still refuses
44+
# to start the dev server without an explicit HOST.
45+
config.action_mailer.default_url_options = {host: ENV.fetch("HOST", "localhost"), port: ENV.fetch("PORT", 3000).to_i}
4246

4347
# Print deprecation notices to the Rails logger.
4448
config.active_support.deprecation = :log

0 commit comments

Comments
 (0)