diff options
author | EuAndreh <eu@euandre.org> | 2022-08-15 17:57:11 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-08-15 17:57:11 -0300 |
commit | 2ca12fa2407f6321d9097696679661629bd4a878 (patch) | |
tree | bb9ffc650855430ac25ea7fb990cce23e87df5e5 /bin | |
parent | bin/uri: Fail when bad option is given (diff) | |
download | dotfiles-2ca12fa2407f6321d9097696679661629bd4a878.tar.gz dotfiles-2ca12fa2407f6321d9097696679661629bd4a878.tar.xz |
bin/serve: Add working version
Diffstat (limited to 'bin')
-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" |