| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| set -euo pipefail |
|
|
| PORT="${PORT:-8181}" |
| ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| LOG_DIR="${TMPDIR:-/tmp}" |
| UVICORN_LOG="$LOG_DIR/mcx-uvicorn.log" |
| TUNNEL_LOG="$LOG_DIR/mcx-tunnel.log" |
|
|
| cd "$ROOT" |
|
|
| |
| pkill -f "uvicorn backend.main:app.*--port $PORT" 2>/dev/null || true |
| pkill -f "cloudflared tunnel --url http://127.0.0.1:$PORT" 2>/dev/null || true |
| sleep 1 |
|
|
| |
| echo "βΆ Starting FastAPI on http://127.0.0.1:$PORT β¦" |
| env -u PYTHONPATH -u VIRTUAL_ENV -u PYTHONHOME \ |
| ".venv/bin/python" -m uvicorn backend.main:app \ |
| --host 127.0.0.1 --port "$PORT" \ |
| > "$UVICORN_LOG" 2>&1 & |
| UVICORN_PID=$! |
|
|
| cleanup() { |
| echo |
| echo "βΆ Shutting down (uvicorn=$UVICORN_PID, cloudflared=${CF_PID:-n/a})β¦" |
| [[ -n "${CF_PID:-}" ]] && kill "$CF_PID" 2>/dev/null || true |
| kill "$UVICORN_PID" 2>/dev/null || true |
| wait 2>/dev/null || true |
| echo "β Stopped. Logs preserved at:" |
| echo " $UVICORN_LOG" |
| echo " $TUNNEL_LOG" |
| } |
| trap cleanup EXIT INT TERM |
|
|
| |
| printf " waiting for ML model load " |
| for _ in $(seq 1 40); do |
| if curl -sf --max-time 1 --noproxy '*' "http://127.0.0.1:$PORT/api/health" >/dev/null 2>&1; then |
| echo " β" |
| break |
| fi |
| printf "." |
| sleep 1 |
| done |
|
|
| if ! curl -sf --max-time 1 --noproxy '*' "http://127.0.0.1:$PORT/api/health" >/dev/null 2>&1; then |
| echo |
| echo "β FastAPI did not become ready in 40 s. Last log lines:" |
| tail -20 "$UVICORN_LOG" |
| exit 1 |
| fi |
|
|
| HEALTH=$(curl -s --noproxy '*' "http://127.0.0.1:$PORT/api/health") |
| ML_LOADED=$(echo "$HEALTH" | python3 -c 'import json,sys; print(json.load(sys.stdin)["ml_loaded"])' 2>/dev/null || echo "?") |
| echo " ML model loaded: $ML_LOADED (response: ${HEALTH:0:80}β¦)" |
| echo |
|
|
| |
| echo "βΆ Opening Cloudflare Quick Tunnel β¦" |
| echo " (your public URL will print below as 'https://*.trycloudflare.com')" |
| echo " βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ" |
|
|
| |
| ./.local/bin/cloudflared tunnel --url "http://127.0.0.1:$PORT" 2>&1 | tee "$TUNNEL_LOG" & |
| CF_PID=$! |
| wait "$CF_PID" |
|
|