Skip to content

Instantly share code, notes, and snippets.

@eastlondoner
Created May 26, 2019 10:40
Show Gist options
  • Save eastlondoner/615acdaad2167f5e0a166f23bebbafd4 to your computer and use it in GitHub Desktop.
Save eastlondoner/615acdaad2167f5e0a166f23bebbafd4 to your computer and use it in GitHub Desktop.
Publish a local directory to ngrok.io using ngrok and python
#!/usr/bin/env bash
# Bail on errors
set -e
# Default to using port 8080 if PORT is not set
PORT="${PORT:-8080}"
echo >&2 "Using port ${PORT}"
# Default to using the US region if REGION is not set
# us - United States (Dallas), eu - Europe (Frankfurt), ap - Asia/Pacific (Singapore), au - Australia (Sydney)
REGION="${REGION:-us}"
echo >&2 "Using ngrok servers in ${REGION}"
# Make sure that when the scripe exits it kills any processes that we backgrounded
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
# change dir into the data dir
pushd data
# Check which python major version is available and start a simple http server accordingly
if [[ "$(python -c 'import sys; print(sys.version_info[0])')" == "3"* ]]; then
python -m http.server "${PORT}" &
else
python -m SimpleHTTPServer "${PORT}" &
fi
echo >&2 "Hosting files in '$(pwd)' on http://localhost:${PORT}"
popd
ngrok http -log=stderr -region="${REGION}" -inspect=false "${PORT}" &
# wait 5 seconds for ngrok to start up
sleep 5
# curl the ngrok client's API and extract the public url
PUBLIC_URL="$(curl --silent http://localhost:4040/api/tunnels/command_line | jq --raw-output .public_url)"
echo >&2 "Tunneled port ${PORT} to ${PUBLIC_URL}."
echo "${PUBLIC_URL}"
# Loop until the user kills us
echo -e >&2 "\n -------------- Success! -------------- \n $(pwd)/data is published to ${PUBLIC_URL}.\n To stop kill this process (Ctrl+c). \n\n"
while true; do
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment