|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Configure the local dev environment for testing on a physical mobile device. |
| 4 | +
|
| 5 | +Updates API URLs and CORS settings across four config files so your phone can |
| 6 | +reach your dev machine over the local network. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 scripts/dev-mobile-setup.py # auto-detect IP |
| 10 | + python3 scripts/dev-mobile-setup.py 192.168.x.x # use a specific IP |
| 11 | + python3 scripts/dev-mobile-setup.py --restore # undo all changes |
| 12 | +
|
| 13 | +Files modified: |
| 14 | + app/proxy/envoy.yaml - adds IP to CORS allow list |
| 15 | + app/backend.dev.env - sets COOKIE_DOMAIN and media server URLs |
| 16 | + app/web/.env.localdev - sets API and media URLs |
| 17 | + app/mobile/.env - switches from staging to local dev mode |
| 18 | +
|
| 19 | +Note: do NOT commit the envoy.yaml change. |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import re |
| 24 | +import socket |
| 25 | +import sys |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +APP = Path(__file__).resolve().parent.parent |
| 29 | + |
| 30 | +ENVOY_YAML = APP / "proxy" / "envoy.yaml" |
| 31 | +BACKEND_ENV = APP / "backend.dev.env" |
| 32 | +WEB_ENV = APP / "web" / ".env.localdev" |
| 33 | +MOBILE_ENV = APP / "mobile" / ".env" |
| 34 | + |
| 35 | +MOBILE_ENV_DEFAULT = """\ |
| 36 | +# STAGE: |
| 37 | +EXPO_PUBLIC_COUCHERS_ENV=preview |
| 38 | +EXPO_PUBLIC_WEB_BASE_URL="https://next.couchershq.org" |
| 39 | +EXPO_PUBLIC_API_BASE_URL="https://dev-api.couchershq.org" |
| 40 | +
|
| 41 | +# LOCAL: |
| 42 | +# EXPO_PUBLIC_COUCHERS_ENV=dev |
| 43 | +# EXPO_PUBLIC_API_BASE_URL="http://[[YOUR_WEB_IP_ADDRESS]]:8888" |
| 44 | +# EXPO_PUBLIC_WEB_BASE_URL="http://[[YOUR_WEB_IP_ADDRESS]]:3000" |
| 45 | +""" |
| 46 | + |
| 47 | + |
| 48 | +def get_local_ip() -> str: |
| 49 | + # Connect to an external address to discover which interface the OS would |
| 50 | + # use — no data is actually sent. |
| 51 | + try: |
| 52 | + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: |
| 53 | + s.connect(("8.8.8.8", 80)) |
| 54 | + return s.getsockname()[0] |
| 55 | + except Exception: |
| 56 | + return "" |
| 57 | + |
| 58 | + |
| 59 | +def setup(ip: str) -> None: |
| 60 | + print(f"Configuring local mobile dev with IP: {ip}\n") |
| 61 | + |
| 62 | + # envoy.yaml — add IP to CORS allow list after the localhost:3000 line |
| 63 | + content = ENVOY_YAML.read_text() |
| 64 | + entry = f"- exact: http://{ip}:3000 # mobile-dev" |
| 65 | + if f"http://{ip}:3000" in content: |
| 66 | + print(" envoy.yaml already configured, skipping") |
| 67 | + else: |
| 68 | + content = content.replace( |
| 69 | + "- exact: http://localhost:3000", |
| 70 | + f"- exact: http://localhost:3000\n {entry}", |
| 71 | + ) |
| 72 | + ENVOY_YAML.write_text(content) |
| 73 | + print(" envoy.yaml added IP to CORS allow list") |
| 74 | + |
| 75 | + # backend.dev.env — COOKIE_DOMAIN + media server URLs |
| 76 | + content = BACKEND_ENV.read_text() |
| 77 | + content = re.sub(r"^COOKIE_DOMAIN=.*", f"COOKIE_DOMAIN={ip}", content, flags=re.MULTILINE) |
| 78 | + content = re.sub(r"^MEDIA_SERVER_BASE_URL=.*", f"MEDIA_SERVER_BASE_URL=http://{ip}:5001", content, flags=re.MULTILINE) |
| 79 | + content = re.sub(r"^MEDIA_SERVER_UPLOAD_BASE_URL=.*", f"MEDIA_SERVER_UPLOAD_BASE_URL=http://{ip}:5001", content, flags=re.MULTILINE) |
| 80 | + BACKEND_ENV.write_text(content) |
| 81 | + print(" backend.dev.env updated COOKIE_DOMAIN and media server URLs") |
| 82 | + |
| 83 | + # web/.env.localdev — API + media URLs |
| 84 | + content = WEB_ENV.read_text() |
| 85 | + content = re.sub(r'^NEXT_PUBLIC_API_BASE_URL=.*', f'NEXT_PUBLIC_API_BASE_URL="http://{ip}:8888"', content, flags=re.MULTILINE) |
| 86 | + content = re.sub(r'^NEXT_PUBLIC_MEDIA_BASE_URL=.*', f'NEXT_PUBLIC_MEDIA_BASE_URL="http://{ip}:5001"', content, flags=re.MULTILINE) |
| 87 | + WEB_ENV.write_text(content) |
| 88 | + print(" web/.env.localdev updated API and media URLs") |
| 89 | + |
| 90 | + # mobile/.env — comment out STAGE block, activate LOCAL block with real IP |
| 91 | + MOBILE_ENV.write_text( |
| 92 | + f"# STAGE:\n" |
| 93 | + f"# EXPO_PUBLIC_COUCHERS_ENV=preview\n" |
| 94 | + f"# EXPO_PUBLIC_WEB_BASE_URL=\"https://next.couchershq.org\"\n" |
| 95 | + f"# EXPO_PUBLIC_API_BASE_URL=\"https://dev-api.couchershq.org\"\n" |
| 96 | + f"\n" |
| 97 | + f"# LOCAL:\n" |
| 98 | + f"EXPO_PUBLIC_COUCHERS_ENV=dev\n" |
| 99 | + f"EXPO_PUBLIC_API_BASE_URL=\"http://{ip}:8888\"\n" |
| 100 | + f"EXPO_PUBLIC_WEB_BASE_URL=\"http://{ip}:3000\"\n" |
| 101 | + ) |
| 102 | + print(" mobile/.env switched to local dev mode") |
| 103 | + |
| 104 | + print( |
| 105 | + "\nDone! Next steps:\n" |
| 106 | + " 1. Restart the backend: docker compose up --build\n" |
| 107 | + " 2. Restart the frontend: cd app/web && yarn start\n" |
| 108 | + " 3. Start Expo: cd app/mobile && npx expo start\n" |
| 109 | + "\n" |
| 110 | + "To undo: python3 scripts/dev-mobile-setup.py --restore\n" |
| 111 | + "\n" |
| 112 | + "Reminder: do not commit the envoy.yaml change." |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def restore() -> None: |
| 117 | + print("Restoring files to defaults...\n") |
| 118 | + |
| 119 | + # envoy.yaml — remove the mobile-dev line |
| 120 | + content = ENVOY_YAML.read_text() |
| 121 | + lines = [line for line in content.splitlines(keepends=True) if "# mobile-dev" not in line] |
| 122 | + ENVOY_YAML.write_text("".join(lines)) |
| 123 | + print(" envoy.yaml removed mobile dev CORS entry") |
| 124 | + |
| 125 | + # backend.dev.env — back to localhost |
| 126 | + content = BACKEND_ENV.read_text() |
| 127 | + content = re.sub(r"^COOKIE_DOMAIN=.*", "COOKIE_DOMAIN=localhost", content, flags=re.MULTILINE) |
| 128 | + content = re.sub(r"^MEDIA_SERVER_BASE_URL=.*", "MEDIA_SERVER_BASE_URL=http://localhost:5001", content, flags=re.MULTILINE) |
| 129 | + content = re.sub(r"^MEDIA_SERVER_UPLOAD_BASE_URL=.*", "MEDIA_SERVER_UPLOAD_BASE_URL=http://localhost:5001", content, flags=re.MULTILINE) |
| 130 | + BACKEND_ENV.write_text(content) |
| 131 | + print(" backend.dev.env restored to localhost") |
| 132 | + |
| 133 | + # web/.env.localdev — back to localhost |
| 134 | + content = WEB_ENV.read_text() |
| 135 | + content = re.sub(r'^NEXT_PUBLIC_API_BASE_URL=.*', 'NEXT_PUBLIC_API_BASE_URL="http://localhost:8888"', content, flags=re.MULTILINE) |
| 136 | + content = re.sub(r'^NEXT_PUBLIC_MEDIA_BASE_URL=.*', 'NEXT_PUBLIC_MEDIA_BASE_URL="http://localhost:5001"', content, flags=re.MULTILINE) |
| 137 | + WEB_ENV.write_text(content) |
| 138 | + print(" web/.env.localdev restored to localhost") |
| 139 | + |
| 140 | + # mobile/.env — back to staging mode |
| 141 | + MOBILE_ENV.write_text(MOBILE_ENV_DEFAULT) |
| 142 | + print(" mobile/.env restored to staging mode") |
| 143 | + |
| 144 | + print("\nDone. Restart backend and frontend to apply.") |
| 145 | + |
| 146 | + |
| 147 | +def main() -> None: |
| 148 | + parser = argparse.ArgumentParser( |
| 149 | + description="Set up local mobile dev environment for physical device testing.", |
| 150 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 151 | + epilog=( |
| 152 | + "examples:\n" |
| 153 | + " python3 scripts/dev-mobile-setup.py\n" |
| 154 | + " python3 scripts/dev-mobile-setup.py 192.168.1.42\n" |
| 155 | + " python3 scripts/dev-mobile-setup.py --restore" |
| 156 | + ), |
| 157 | + ) |
| 158 | + parser.add_argument("ip", nargs="?", help="local IP address (auto-detected if omitted)") |
| 159 | + parser.add_argument("--restore", action="store_true", help="revert all changes to defaults") |
| 160 | + args = parser.parse_args() |
| 161 | + |
| 162 | + if args.restore: |
| 163 | + restore() |
| 164 | + return |
| 165 | + |
| 166 | + ip = args.ip or get_local_ip() |
| 167 | + if not ip: |
| 168 | + print("Error: could not detect local IP. Pass it explicitly:") |
| 169 | + print(" python3 scripts/dev-mobile-setup.py 192.168.x.x") |
| 170 | + sys.exit(1) |
| 171 | + |
| 172 | + setup(ip) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + main() |
0 commit comments