|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | # Script: run-front.sh |
4 | | -# Does: Starts Expo frontend and lets you choose backend target (Production or Local). |
5 | | -# Use: ./run-front.sh [--local|--production] [--clear] [--dev-client] |
| 4 | +# Does: Starts Expo frontend and lets you choose backend target (Local or Live). |
| 5 | +# Use: ./run-front.sh [--local|--live|--production] [--clear] [--dev-client] |
6 | 6 | # DB: None (frontend only). |
7 | 7 | # Ports: Metro uses LAN IP; local API defaults to :8080 (EXPO_PUBLIC_LOCAL_API_PORT overrides). |
8 | 8 | # Modes: Frontend=ON | Backend=Production URL or Local URL | Live stack untouched | Dev/Test DB untouched. |
@@ -31,26 +31,53 @@ for arg in "$@"; do |
31 | 31 | --production) |
32 | 32 | MODE="production" |
33 | 33 | ;; |
| 34 | + --live) |
| 35 | + MODE="production" |
| 36 | + ;; |
34 | 37 | *) |
35 | 38 | echo "Unknown argument: $arg" |
36 | | - echo "Usage: ./run-front.sh [--local|--production] [--clear] [--dev-client]" |
| 39 | + echo "Usage: ./run-front.sh [--local|--live|--production] [--clear] [--dev-client]" |
37 | 40 | exit 1 |
38 | 41 | ;; |
39 | 42 | esac |
40 | 43 | done |
41 | 44 |
|
42 | 45 | detect_lan_ip() { |
43 | | - hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^10\.|^192\.168\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.' | head -n1 |
| 46 | + local from_route="" |
| 47 | + local iface="" |
| 48 | + |
| 49 | + if command -v ip >/dev/null 2>&1; then |
| 50 | + from_route="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i=1; i<=NF; i++) if ($i=="src") {print $(i+1); exit}}')" |
| 51 | + if [[ "$from_route" =~ ^10\.|^192\.168\.|^172\.(1[6-9]|2[0-9]|3[0-1])\. ]]; then |
| 52 | + echo "$from_route" |
| 53 | + return 0 |
| 54 | + fi |
| 55 | + |
| 56 | + iface="$(ip route 2>/dev/null | awk '/^default/ {print $5; exit}')" |
| 57 | + if [[ -n "$iface" ]]; then |
| 58 | + ip -4 addr show dev "$iface" scope global 2>/dev/null | |
| 59 | + awk '/inet / {print $2}' | |
| 60 | + cut -d/ -f1 | |
| 61 | + grep -E '^10\.|^192\.168\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.' | |
| 62 | + head -n1 |
| 63 | + return 0 |
| 64 | + fi |
| 65 | + fi |
| 66 | + |
| 67 | + hostname -I 2>/dev/null | |
| 68 | + tr ' ' '\n' | |
| 69 | + grep -E '^10\.|^192\.168\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.' | |
| 70 | + head -n1 |
44 | 71 | } |
45 | 72 |
|
46 | 73 | LAN_IP="$(detect_lan_ip || true)" |
47 | 74 | if [[ -z "$LAN_IP" ]]; then |
48 | 75 | LAN_IP="127.0.0.1" |
49 | | - echo "Warning: Could not detect private LAN IPv4. Falling back to 127.0.0.1." |
| 76 | + echo "Warning: Could not detect private LAN IPv4." |
50 | 77 | fi |
51 | 78 |
|
52 | 79 | if [[ -z "$MODE" ]]; then |
53 | | - echo "Select backend: 1) Production (devbits.ddns.net) 2) Local (LAN IP:8080)" |
| 80 | + echo "Select backend: 1) Live (devbits.app) 2) Local (LAN IP:8080)" |
54 | 81 | read -r -p "Choose [1/2]: " selection |
55 | 82 | case "$selection" in |
56 | 83 | 1) MODE="production" ;; |
|
69 | 96 | unset REACT_NATIVE_PACKAGER_HOSTNAME || true |
70 | 97 | unset EXPO_PACKAGER_HOSTNAME || true |
71 | 98 | fi |
72 | | -export EXPO_PUBLIC_API_URL="https://devbits.ddns.net" |
73 | | -export EXPO_PUBLIC_API_FALLBACK_URL="https://devbits.ddns.net" |
| 99 | +export EXPO_PUBLIC_API_URL="https://devbits.app" |
| 100 | +export EXPO_PUBLIC_API_FALLBACK_URL="https://devbits.app" |
74 | 101 |
|
75 | 102 | if [[ "$MODE" == "local" ]]; then |
76 | 103 | LOCAL_API_PORT="${EXPO_PUBLIC_LOCAL_API_PORT:-8080}" |
| 104 | + |
| 105 | + if [[ "$LAN_IP" == "127.0.0.1" && "${DEVBITS_ALLOW_LOOPBACK_LOCAL_API:-0}" != "1" ]]; then |
| 106 | + echo "Error: Local mode resolved loopback (127.0.0.1), which will fail on physical devices." |
| 107 | + echo "Fix one of the following and try again:" |
| 108 | + echo " 1) Run without sudo so network detection can read your user network context." |
| 109 | + echo " 2) Set EXPO_PUBLIC_LOCAL_API_URL manually, e.g. http://192.168.x.y:${LOCAL_API_PORT}." |
| 110 | + echo " 3) If using only simulator/emulator intentionally, set DEVBITS_ALLOW_LOOPBACK_LOCAL_API=1." |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + |
77 | 114 | export EXPO_PUBLIC_USE_LOCAL_API=1 |
78 | | - export EXPO_PUBLIC_LOCAL_API_URL="http://${LAN_IP}:${LOCAL_API_PORT}" |
| 115 | + if [[ -z "${EXPO_PUBLIC_LOCAL_API_URL:-}" ]]; then |
| 116 | + export EXPO_PUBLIC_LOCAL_API_URL="http://${LAN_IP}:${LOCAL_API_PORT}" |
| 117 | + fi |
79 | 118 | echo "Using local backend: $EXPO_PUBLIC_LOCAL_API_URL" |
80 | 119 | else |
81 | 120 | export EXPO_PUBLIC_USE_LOCAL_API=0 |
82 | 121 | unset EXPO_PUBLIC_LOCAL_API_URL || true |
83 | | - echo "Using production backend: https://devbits.ddns.net" |
| 122 | + echo "Using live backend: https://devbits.app" |
84 | 123 | fi |
85 | 124 |
|
86 | 125 | cd "$FRONTEND_DIR" |
|
0 commit comments