clone

a personal forge on plain git: static pages, stacked-diff reviews, signed local ci, repo sites and releases

main
forge / test / scenarios / private-web-caddy.sh
private-web-caddy.sh 3.9K
1# Caddy serves a file of www when there is one and hands every other path2# to the private server, so it needs no list of private repos. With caddy3# installed, the real thing: public pages come from www with Caddy's cache4# headers, a private page goes to its reader with a token, and anyone else5# gets the same response as for a page that isn't there. A comment posted6# to a public repo's review, whose pages are static files, reaches the7# private server as well.8. "$(dirname "$0")/../lib.sh"9user dami10user cat11repo --private dami/secret "hidden thing"12repo dami/open13for r in secret open; do14 clone dami "dami/$r" "$T/w/$r"15 at "$T/w/$r" commit "first"16 ok push_as dami "$T/w/$r" origin main17done18at "$T/w/open" git switch -q -c dami/feature19at "$T/w/open" commit "feature"20ok push_as dami "$T/w/open" origin dami/feature21build2223ok forge server caddyfile git.test24cp "$T_LOG" "$T/Caddyfile"25has "$T/Caddyfile" '^ @public file \{$'26has "$T/Caddyfile" '^ try_files \{path\} \{path\}/$'27has "$T/Caddyfile" '^ reverse_proxy 127\.0\.0\.1:8093$'28lacks "$T/Caddyfile" 'private/www'29# The private server is the last handler: only what nothing else took.30eq "$(grep -n 'reverse_proxy 127.0.0.1:8093' "$T/Caddyfile" | tail -n 1 | cut -d: -f1)" "$(($(grep -n '^}$' "$T/Caddyfile" | head -n 1 | cut -d: -f1) - 2))"31# And comments and approvals, before www's files.32has "$T/Caddyfile" '^ path_regexp \^/\[\^/\]\+/\[\^/\]\+/reviews/\[0-9\]\+/\(comment\|approve\)\$$'33[ "$(grep -n '^ handle @discuss {$' "$T/Caddyfile" | cut -d: -f1)" -lt "$(grep -n '^ handle @public {$' "$T/Caddyfile" | cut -d: -f1)" ] || fail "@discuss comes after @public"34command -v caddy >/dev/null 2>&1 || { note "caddy is not installed: skipped running it"; exit 0; }35ok caddy adapt --config "$T/Caddyfile" --adapter caddyfile3637cport=$(free_port)38private_server39{40 printf '{\n\tadmin off\n\tauto_https off\n\tpersist_config off\n}\n\n'41 FORGE_PRIVATE_PORT=$PRIV_PORT "$FORGE" server caddyfile "http://127.0.0.1:$cport"42} >"$T/Caddyfile.run"43XDG_DATA_HOME=$T/xdg XDG_CONFIG_HOME=$T/xdg caddy run --config "$T/Caddyfile.run" --adapter caddyfile >"$T/caddy.log" 2>&1 &44T_PIDS="$T_PIDS $!"45c=http://127.0.0.1:$cport46wait_http "$c/"47dami=$(ssh_as dami token 2>/dev/null)48cat=$(ssh_as cat token 2>/dev/null)4950response "$c/dami/open/" >"$T/open"51has "$T/open" '^HTTP/1.1 200 OK'52has "$T/open" '^Cache-Control: no-cache'53style=$(grep -o '/assets/style\.[^"]*\.css' "$T/open" | head -n 1)54response "$c$style" -I >"$T/style"55has "$T/style" '^Cache-Control: public, max-age=31536000, immutable'5657response "$c/dami/secret/" -H "Authorization: Bearer $dami" >"$T/secret"58has "$T/secret" '^HTTP/1.1 200 OK'59has "$T/secret" 'hidden thing'60eq "$(grep -c '^Cache-Control:' "$T/secret")" 161has "$T/secret" '^Cache-Control: private, no-cache'62response "$c/dami/secret/code.db" -H "Authorization: Bearer $dami" -H 'Range: bytes=0-99' -H 'Accept-Encoding: gzip' -o /dev/null -D - >"$T/db"63has "$T/db" '^HTTP/1.1 206 Partial Content'64has "$T/db" '^Content-Range: bytes 0-99/'65lacks "$T/db" '^Content-Encoding'6667response "$c/dami/nope/" >"$T/missing"68has "$T/missing" '^HTTP/1.1 404 Not Found'69same() {70 response "$@" >"$T/got"71 cmp -s "$T/missing" "$T/got" || { diff "$T/missing" "$T/got" >&2 || true; fail "through Caddy, $* is not the missing page's 404"; }72}73same "$c/dami/secret/"74same "$c/dami/secret/" -H "Authorization: Bearer $cat"75same "$c/dami/open/nope/"76same "$c/nobody/x/y/"7778# A comment on a public review, through Caddy, with a token.79exists "$FORGE_WWW/dami/open/reviews/1/index.html"80eq "$(http -s -o "$T/res" -w '%{http_code}' -H "Authorization: Bearer $cat" -d 'body=Through Caddy' "$c/dami/open/reviews/1/comment")" 20081has "$T/res" '"ok":true,"message":"comment 1 on #1'82has "$FORGE_ROOT/dami/open.git/forge/comments/1/1" '^Through Caddy$'83eq "$(http -s -o "$T/res" -w '%{http_code}' -d 'body=Signed out' "$c/dami/open/reviews/1/comment")" 40184same "$c/dami/secret/reviews/1/comment" -X POST -H "Authorization: Bearer $cat" -d 'body=hi'