backnforth/setup.sh

29 lines
911 B
Bash

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
if command -v python3 >/dev/null 2>&1; then PY=python3; elif command -v python >/dev/null 2>&1; then PY=python; else echo "Python not found"; exit 1; fi
if [ ! -d ".venv" ]; then "$PY" -m venv .venv; fi
VENV_PY="./.venv/bin/python"
"$VENV_PY" -m pip install -U pip websockets
IP="$($VENV_PY - <<'PYCODE'
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8',80))
print(s.getsockname()[0])
except Exception:
print('localhost')
finally:
s.close()
PYCODE
)"
# Start static server if not running
if ! lsof -ti:8000 >/dev/null 2>&1; then nohup "$VENV_PY" -m http.server 8000 >/dev/null 2>&1 & fi
URL="http://$IP:8000/"
case "$(uname -s)" in
Darwin) open "$URL" ;;
Linux) command -v xdg-open >/dev/null 2>&1 && xdg-open "$URL" || true ;;
esac
echo "Relay URL: ws://$IP:8787"
exec "$VENV_PY" server.py