diff options
Diffstat (limited to 'bin/serve')
-rwxr-xr-x | bin/serve | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/bin/serve b/bin/serve new file mode 100755 index 0000000..4a06c50 --- /dev/null +++ b/bin/serve @@ -0,0 +1,78 @@ +#!/bin/sh +set -eu + + +usage() { + cat <<-'EOF' + Usage: + serve [-d DIRECTORY] [-p PORT] + serve -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -d DIRECTORY the directory to serve (default: ".") + -p PORT the port to listen on (default: 8000) + -h, --help show this message + + + Serve DIRECTORY via HTTP as a static file server, and open the + URL on the $BROWSER. + + + Examples: + + Serve "." on the default PORT: + + $ serve + + + Serve "public/" on port 1234: + + $ serve -d public/ -p 1234 + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +DIRECTORY='.' +PORT=8000 +while getopts 'd:p:h' flag; do + case "$flag" in + d) + DIRECTORY="$OPTARG" + ;; + p) + PORT="$OPTARG" + ;; + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done + +open "http://localhost:$PORT" +python3 -m http.server -d "$DIRECTORY" "$PORT" |