memlnaut-nisps/playground/serve.sh
2026-03-04 20:47:06 +00:00

18 lines
547 B
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Serve the NISPS playground, finding an open port if needed.
PORT="${1:-8000}"
MAX_ATTEMPTS=20
DIR="$(cd "$(dirname "$0")" && pwd)"
for ((i = 0; i < MAX_ATTEMPTS; i++)); do
candidate=$((PORT + i))
if ! ss -tlnp 2>/dev/null | grep -q ":${candidate} "; then
echo "Serving playground at http://localhost:${candidate}"
exec python3 -m http.server "$candidate" -d "$DIR"
fi
echo "Port ${candidate} in use, trying next..."
done
echo "No open port found in range ${PORT}$((PORT + MAX_ATTEMPTS - 1))" >&2
exit 1