-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-dev.sh
More file actions
118 lines (101 loc) · 3.69 KB
/
run-dev.sh
File metadata and controls
118 lines (101 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
# Script: run-dev.sh
# Does: Boots local dev DB + local backend (isolated compose project) only.
# Use: ./run-dev.sh
# DB: devbits_dev (user/pass: devbits_dev/devbits_dev_password) in compose project devbits-dev-local.
# Ports: backend default :8080, DB default :5433 (DEVBITS_BACKEND_PORT / DEVBITS_DB_PORT override).
# Modes: Frontend=OFF | Backend=ON(local Docker) | Live stack untouched | Test DB untouched.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BACKEND_DIR="$ROOT/backend"
COMPOSE_PROJECT="devbits-dev-local"
if [[ "${EUID}" -eq 0 ]]; then
echo "Warning: Running as root is not recommended for Expo local mode."
echo "Use ./run-dev.sh (without sudo) so LAN IP detection and device connectivity work reliably."
fi
if ! command -v docker >/dev/null 2>&1; then
echo "Error: Docker is required."
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
echo "Error: Docker Compose v2 is required."
exit 1
fi
is_port_in_use() {
local port="$1"
if command -v ss >/dev/null 2>&1; then
ss -ltn "sport = :${port}" | grep -q LISTEN
return $?
fi
if command -v lsof >/dev/null 2>&1; then
lsof -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1
return $?
fi
netstat -an 2>/dev/null | grep -E "[:.]${port}[[:space:]]" | grep -qi LISTEN
}
resolve_port() {
local label="$1"
local default_port="$2"
local chosen="$default_port"
while is_port_in_use "$chosen"; do
echo "Port ${chosen} is already in use for ${label}."
read -r -p "Enter alternate port for ${label} (blank to exit): " chosen
if [[ -z "$chosen" ]]; then
echo "Exiting. Free port ${default_port} or choose an alternate port next run."
exit 1
fi
if ! [[ "$chosen" =~ ^[0-9]+$ ]] || (( chosen < 1 || chosen > 65535 )); then
echo "Invalid port: ${chosen}"
chosen="$default_port"
fi
done
echo "$chosen"
}
DEVBITS_BACKEND_PORT="$(resolve_port "backend" 8080)"
DEVBITS_DB_PORT="$(resolve_port "postgres" 5433)"
export DEVBITS_BACKEND_PORT
export DEVBITS_DB_PORT
echo "Using backend port ${DEVBITS_BACKEND_PORT} and db port ${DEVBITS_DB_PORT}."
cd "$BACKEND_DIR"
docker compose -p "$COMPOSE_PROJECT" -f docker-compose.dev.yml down --volumes --remove-orphans
docker compose -p "$COMPOSE_PROJECT" -f docker-compose.dev.yml up -d --build
echo "Waiting for database readiness..."
for i in $(seq 1 60); do
if docker compose -p "$COMPOSE_PROJECT" -f docker-compose.dev.yml exec -T db pg_isready -U devbits_dev -d devbits_dev >/dev/null 2>&1; then
echo "Database is ready."
break
fi
if [[ "$i" -eq 60 ]]; then
echo "Error: Database did not become ready within 60 seconds."
exit 1
fi
sleep 1
done
echo "Waiting for backend health check..."
for i in $(seq 1 60); do
if command -v curl >/dev/null 2>&1; then
if curl -fsS "http://localhost:${DEVBITS_BACKEND_PORT}/health" >/dev/null 2>&1; then
echo "Backend is healthy."
break
fi
elif command -v wget >/dev/null 2>&1; then
if wget -q -O /dev/null "http://localhost:${DEVBITS_BACKEND_PORT}/health"; then
echo "Backend is healthy."
break
fi
fi
if [[ "$i" -eq 60 ]]; then
echo "Error: Backend did not become healthy within 60 seconds."
docker compose -p "$COMPOSE_PROJECT" -f docker-compose.dev.yml logs backend --tail 100
exit 1
fi
sleep 1
done
echo
echo "Local backend stack is ready."
echo "Backend health: http://localhost:${DEVBITS_BACKEND_PORT}/health"
echo "To launch frontend against local backend, run:"
echo " EXPO_PUBLIC_LOCAL_API_PORT=${DEVBITS_BACKEND_PORT} $SCRIPT_DIR/run-front.sh --local"
echo "To launch frontend against live backend, run:"
echo " $SCRIPT_DIR/run-front.sh --live"