-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathstart-dashboard.sh
More file actions
executable file
·141 lines (121 loc) · 4.46 KB
/
Copy pathstart-dashboard.sh
File metadata and controls
executable file
·141 lines (121 loc) · 4.46 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
set -euo pipefail
# Start the Next.js dashboard (and the Python backend if not already running).
# Usage: ./start-dashboard.sh
# Env:
# PORT (frontend) default 6969
# BACKEND_PORT default 6970
# BACKEND_HOST default localhost
# SKIP_BACKEND=1 to skip starting backend
# LOG_FILE (default /tmp/nextjs-dashboard.log)
# BACKEND_LOG_FILE (default /tmp/dashboard-backend.log)
# CLEAN_CACHE (default 1), RUN_BUILD (default 0), TAIL_LOG (default 1)
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
APP_DIR="$ROOT_DIR/dashboard/web"
LOG_FILE="${LOG_FILE:-/tmp/nextjs-dashboard.log}"
BACKEND_LOG_FILE="${BACKEND_LOG_FILE:-/tmp/dashboard-backend.log}"
CLEAN_CACHE="${CLEAN_CACHE:-1}" # default: wipe .next/.turbo caches before start
RUN_BUILD="${RUN_BUILD:-0}" # default: skip prod build for faster dev iteration
TAIL_LOG="${TAIL_LOG:-1}" # default: tail the dev log after start
BACKEND_HOST="${BACKEND_HOST:-127.0.0.1}" # default to IPv4 to avoid IPv6 localhost issues
BACKEND_PORT="${BACKEND_PORT:-6970}"
SKIP_BACKEND="${SKIP_BACKEND:-0}" # set to 1 to skip auto-starting backend
BACKEND_PID=""
TAIL_PID=""
# Export backend host/port so Next.js picks up the same values for proxy rewrites.
export BACKEND_HOST BACKEND_PORT
export NEXT_PUBLIC_BACKEND_HOST="${BACKEND_HOST}"
export NEXT_PUBLIC_BACKEND_PORT="${BACKEND_PORT}"
# Ensure log files exist before tailing
touch "$LOG_FILE" "$BACKEND_LOG_FILE"
cleanup() {
# Best-effort cleanup; ignore failures
if [ -n "${TAIL_PID:-}" ]; then kill "$TAIL_PID" >/dev/null 2>&1 || true; fi
if [ -n "${DEV_PID:-}" ]; then kill "$DEV_PID" >/dev/null 2>&1 || true; fi
if [ -n "${BACKEND_PID:-}" ]; then kill "$BACKEND_PID" >/dev/null 2>&1 || true; fi
}
trap cleanup EXIT INT TERM
cd "$APP_DIR"
echo "[config] Backend: ${BACKEND_HOST}:${BACKEND_PORT}"
# If npm isn't in PATH, try loading nvm (if installed) before failing.
if ! command -v npm >/dev/null 2>&1; then
NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
if [ -s "${NVM_DIR}/nvm.sh" ]; then
# shellcheck disable=SC1090
. "${NVM_DIR}/nvm.sh"
fi
fi
if ! command -v npm >/dev/null 2>&1; then
echo "[error] npm not found. Please install Node.js 18+ (includes npm) and re-run." >&2
echo " Or run: ./setup-dashboard.sh" >&2
echo " Example (Ubuntu): sudo apt-get update && sudo apt-get install -y nodejs npm" >&2
exit 1
fi
# Install dependencies if needed
if [ ! -d node_modules ]; then
echo "[setup] Installing npm dependencies..."
npm install
fi
# Optional cache wipe
if [ "$CLEAN_CACHE" -eq 1 ]; then
echo "[cleanup] Removing .next/.turbo caches..."
rm -rf .next .turbo node_modules/.cache >/dev/null 2>&1 || true
fi
if [ "$SKIP_BACKEND" -eq 0 ]; then
if python - <<PY
import socket, sys
s = socket.socket()
try:
s.connect(("127.0.0.1", int("${BACKEND_PORT}")))
sys.exit(0)
except Exception:
sys.exit(1)
finally:
s.close()
PY
then
echo "[backend] Detected backend on :${BACKEND_PORT}"
else
echo "[backend] Starting Python server on :${BACKEND_PORT} ..."
echo "[backend] Log: $BACKEND_LOG_FILE"
(cd "$ROOT_DIR" && python -m dashboard.api.server serve --port "${BACKEND_PORT}") >"$BACKEND_LOG_FILE" 2>&1 &
BACKEND_PID=$!
echo "[backend] PID: $BACKEND_PID"
sleep 2
fi
else
echo "[backend] SKIP_BACKEND=1 so skipping backend check/start."
fi
# Run a build to surface compile errors early
if [ "$RUN_BUILD" -eq 1 ]; then
echo "[build] Running npm run build to verify compilation..."
if ! npm run build; then
echo "[build] Build failed. See logs above or $LOG_FILE" >&2
exit 1
fi
fi
# Frontend port (avoid clashing with backend); default 3000
PORT="${PORT:-6969}"
echo "[frontend] Starting Next.js dev server on :$PORT ..."
echo "[frontend] Logs: $LOG_FILE"
export HOST=0.0.0.0
export PORT
if lsof -i :"$PORT" >/dev/null 2>&1; then
echo "[warn] Port $PORT is already in use. If a previous dev server is running, stop it or set PORT=xxxx when invoking this script."
fi
# Run dev server in background, piping to log
npm run dev -- --hostname "$HOST" --port "$PORT" >>"$LOG_FILE" 2>&1 &
DEV_PID=$!
echo "[frontend] Dev server PID: $DEV_PID"
# Tail logs in foreground if requested
if [ "$TAIL_LOG" -eq 1 ]; then
echo "[logs] Tailing frontend log ($LOG_FILE) and backend log ($BACKEND_LOG_FILE)..."
tail -n 0 -F "$LOG_FILE" "$BACKEND_LOG_FILE" &
TAIL_PID=$!
wait "$DEV_PID"
# Ensure tail exits once the dev server stops
kill "$TAIL_PID" >/dev/null 2>&1 || true
wait "$TAIL_PID" || true
else
wait "$DEV_PID"
fi