catcrafts.net/tools/dev.sh

304 lines
14 KiB
Shell
Raw Normal View History

2026-08-05 04:18:37 +02:00
#!/bin/sh
# Run the whole site locally, in the same shape as production.
#
# tools/dev.sh build both products and serve on :8080
# tools/dev.sh --no-build use whatever is already in bin/
#
# Why this exists: catcrafts-server serves PAGES only. Static assets — the wasm
# module, styles.css, the JS bridges — are Caddy's job in production, so running
# the server on its own gives you correct HTML with no stylesheet, which looks
# broken and isn't. This starts both and puts Caddy in front, so what you see
# locally is what the deployed site does, including the reverse-proxy split and
# the scoped cross-origin headers.
#
# Ctrl-C stops both.
set -eu
BUILD=1
[ "${1:-}" = "--no-build" ] && BUILD=0
PORT="${DEV_PORT:-8080}"
BACKEND_PORT="${DEV_BACKEND_PORT:-8081}"
WORK="$(mktemp -d)"
# Refuse to start if either port is taken.
#
# Without this, a leftover instance from an earlier run keeps serving: the new
# backend fails to bind, the old Caddy carries on proxying to the OLD binary, and
# the site looks like the build did not take effect. That has wasted real time
# twice — the symptom (stale content) points at the build, not at a process.
port_busy() {
if command -v ss >/dev/null 2>&1; then
ss -ltn 2>/dev/null | grep -qE "[:.]$1 "
else
curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$1/" 2>/dev/null
fi
}
for _p in "$PORT" "$BACKEND_PORT"; do
if port_busy "$_p"; then
echo "dev: port $_p is already in use — another instance is probably still running." >&2
echo "dev: find it with: ss -ltnp | grep -E ':(8080|8081) '" >&2
echo "dev: then kill those PIDs, or set DEV_PORT / DEV_BACKEND_PORT." >&2
exit 1
fi
done
cleanup() {
[ -n "${SRV_PID:-}" ] && kill "$SRV_PID" 2>/dev/null || true
[ -n "${CADDY_PID:-}" ] && kill "$CADDY_PID" 2>/dev/null || true
[ -n "${TAIL_PID:-}" ] && kill "$TAIL_PID" 2>/dev/null || true
2026-08-05 04:18:37 +02:00
rm -rf "$WORK"
}
trap cleanup EXIT INT TERM
# crafter-build is the Crafter.Build Flatpak. An interactive shell reaches it
# through an alias, which a script does not inherit, so call the Flatpak
# itself unless a crafter-build command is on PATH (CI installs a wrapper).
CRAFTER_REF="${CRAFTER_REF:-net.catcrafts.CrafterBuild}"
crafter_build() {
if command -v crafter-build >/dev/null 2>&1; then
crafter-build "$@"
else
flatpak run --die-with-parent "$CRAFTER_REF" "$@"
fi
}
# What it builds links the SDK's libc++ and libunwind (RUNPATH /app/lib), so the
# server runs inside the Flatpak as well, the way CI's crafter-run does: on the
# host it does not even start (libunwind.so.1 not found). $WORK is under /tmp,
# which the sandbox does not share, hence the grant. Without it the server
# cannot write the order ledger and every checkout answers 500.
in_sdk() {
_cmd=$1; shift
flatpak run --die-with-parent --filesystem="$WORK" --command="$_cmd" \
"$CRAFTER_REF" "$@"
}
# A product's build directory. Its name embeds a config hash, and every
# Crafter.Build update brings a new one, so locally bin/ gains a variant per
# toolchain release while nothing removes the old ones. CI starts from a clean
# bin/ and keeps its exactly-one rule; here, take the variant whose artifact was
# written last, which right after a build is the build just made. When there is
# a choice, say which one won, so a stale pick cannot go unnoticed.
variant() {
_pick=$(ls -t bin/$1/$2 2>/dev/null | head -n 1)
if [ -z "$_pick" ]; then
echo "dev: no bin/$1/$2 found; run without --no-build first" >&2
2026-08-05 04:18:37 +02:00
exit 1
fi
_n=$(ls -d bin/$1 | wc -l)
[ "$_n" -gt 1 ] && echo "dev: using ${_pick%/*} (newest of $_n builds)" >&2
printf '%s' "${_pick%/*}"
2026-08-05 04:18:37 +02:00
}
if [ "$BUILD" = 1 ]; then
echo "dev: building the server product..."
crafter_build --product=server >"$WORK/build-server.log" 2>&1 \
2026-08-05 04:18:37 +02:00
|| { echo "dev: server build failed:" >&2; tail -20 "$WORK/build-server.log" >&2; exit 1; }
SRV=$(variant 'Catcrafts.Server-*' catcrafts-server)
2026-08-05 04:18:37 +02:00
# sitemap.xml and feed.xml are generated from the same route table and Post
# model the pages use, and the wasm build copies them into the bundle — so
# they have to exist before it runs.
in_sdk "$PWD/$SRV/catcrafts-server" --sitemap > sitemap.xml
in_sdk "$PWD/$SRV/catcrafts-server" --feed > feed.xml
2026-08-05 04:18:37 +02:00
echo "dev: building the wasm bundle..."
crafter_build >"$WORK/build-web.log" 2>&1 \
2026-08-05 04:18:37 +02:00
|| { echo "dev: wasm build failed:" >&2; tail -20 "$WORK/build-web.log" >&2; exit 1; }
fi
[ -n "${SRV:-}" ] || SRV=$(variant 'Catcrafts.Server-*' catcrafts-server)
WEB=$(variant 'Catcrafts.Net-*' catcrafts.wasm)
2026-08-05 04:18:37 +02:00
# Makes the static shell survive being served at a deep URL. Run unconditionally,
# not just after a build: --no-build may be pointing at a bundle someone produced
# with a bare crafter-build, and the script is idempotent.
./tools/fix-bundle-depth.sh "$WEB" >/dev/null
ABS_WEB="$(cd "$WEB" && pwd)"
# Mirrored post media. Absent is fine — the pages render, the media 404s — so
# this does not block running the site before fetch-media.sh has been run.
mkdir -p media
PWD_MEDIA="$(cd media && pwd)"
# Mirrors deploy/Caddyfile.example: static assets from disk, everything else
# proxied to the backend, cross-origin isolation only on the paths that boot the
# wasm module.
cat > "$WORK/Caddyfile" <<EOF
:$PORT {
root * $ABS_WEB
encode zstd gzip
@isolated path /demos/* /catcrafts*.wasm /runtime.js /dom-env.js /dom-webgpu.js /catcrafts-head.js /files.json /variants.json /*.wgsl
header @isolated {
Cross-Origin-Opener-Policy "same-origin"
Cross-Origin-Embedder-Policy "require-corp"
Cross-Origin-Resource-Policy "same-origin"
}
2026-08-18 21:21:41 +02:00
rewrite /apple-touch-icon-precomposed.png /apple-touch-icon.png
@static path /catcrafts*.wasm /runtime.js /dom-env.js /dom-webgpu.js /catcrafts-head.js /files.json /variants.json /styles.css /favicon.svg /favicon.ico /apple-touch-icon.png /robots.txt /*.wgsl /*.jpg /posts.json /rates.json
2026-08-05 04:18:37 +02:00
handle @static {
header Cache-Control "no-store"
file_server
}
handle_path /media/* {
root * $PWD_MEDIA
header Cache-Control "no-store"
file_server
}
handle {
reverse_proxy 127.0.0.1:$BACKEND_PORT
}
}
EOF
# Rail selection for dev:
2026-08-20 20:15:47 +02:00
# * a repo-root .env (gitignored, never committed) is sourced if present;
# * DEV_RAIL=transfer|fake overrides the automatic choice;
# * default is TRANSFER, because that is what production serves. Dev used to
# default to the fake rail, which meant the bank rail's actual page — the
# IBAN, the beneficiary name, the reference, the whole thing a buyer reads
# — could not be looked at locally at all.
2026-08-05 04:18:37 +02:00
#
2026-08-20 20:15:47 +02:00
# The transfer rail needs no credential, so dev can run the REAL rail rather
# than a stand-in: it only needs an account to name. If .env does not name one,
# obviously-fake values are used, and they are labelled as such on purpose —
# never make fixture data look real (a sample financials file was once mistaken
# for the actual books).
#
# Nothing here can collect real money: the transfer rail only RENDERS account
# details, and settlement comes from a credits file this machine writes.
2026-08-05 04:18:37 +02:00
if [ -f .env ]; then
set -a; . ./.env; set +a
fi
2026-08-20 20:15:47 +02:00
RAIL="${DEV_RAIL:-transfer}"
if [ "$RAIL" = transfer ]; then
# Refuse a real-looking IBAN that is not yours to be paid into by mistake?
# No — the opposite risk matters here. These values are only ever RENDERED
# in dev; nothing can be paid. What must not happen is dev quietly showing
# the PRODUCTION account while someone screenshots the page, so when .env
# supplies nothing the placeholders say plainly that they are placeholders.
: "${TRANSFER_IBAN:=NL00DEVB0000000000}"
: "${TRANSFER_BENEFICIARY:=DEV PLACEHOLDER, not a real account}"
: "${TRANSFER_BIC:=DEVBNL2A}"
export TRANSFER_IBAN TRANSFER_BENEFICIARY TRANSFER_BIC
if [ "$TRANSFER_IBAN" = NL00DEVB0000000000 ]; then
echo "dev: bank transfer rail with PLACEHOLDER account details"
echo "dev: put TRANSFER_IBAN / TRANSFER_BENEFICIARY / TRANSFER_BIC in .env to preview the real ones"
else
echo "dev: bank transfer rail, account $TRANSFER_IBAN ($TRANSFER_BENEFICIARY)"
fi
2026-08-05 04:18:37 +02:00
fi
2026-08-14 04:14:13 +02:00
# /financials has two inputs and a fresh dev run has neither: sales fold out of
# the order ledger, donations and expenses come from the bank-aggregates file.
# So the page honestly renders its empty state — which is worth previewing too,
# and is why this is opt-in rather than always on.
#
# DEV_FINANCIALS=1 seeds both with obviously-sample figures, so the FILLED
# layout can be designed against without waiting for real money or touching
# production. The aggregates file is re-read on every request, so you can edit
# it while the server runs and just refresh.
#
# DEV_FINANCIALS=<path> instead previews a REAL aggregates file — the one
# staged for production, say. No sample orders are seeded in that case: sales
# fold from the ledger, and inventing them would misrepresent the very figures
# you are checking. A closed shop showing €0 of sales is the truth.
if [ "${DEV_FINANCIALS:-0}" != 0 ] && [ "${DEV_FINANCIALS:-0}" != 1 ]; then
if [ ! -f "$DEV_FINANCIALS" ]; then
echo "dev: DEV_FINANCIALS='$DEV_FINANCIALS' is not a file" >&2
exit 1
fi
cp "$DEV_FINANCIALS" "$WORK/orders.jsonl.financials.json"
echo "dev: previewing REAL financials from $DEV_FINANCIALS (no sample orders seeded)"
elif [ "${DEV_FINANCIALS:-0}" = 1 ]; then
# Labels say SAMPLE and the amounts are flat round numbers on purpose:
# plausible-looking figures here were once mistaken for real bank data.
# Nothing in this block comes from anywhere — it exists to fill the layout.
cat > "$WORK/orders.jsonl.financials.json" <<'JSON'
{"as_of":"2026-01-01",
"donations":{"count":4,"total_minor":10000},
"expenses":[{"label":"SAMPLE hosting","total_minor":10000},
{"label":"SAMPLE insurance","total_minor":20000},
{"label":"SAMPLE inventory","total_minor":300000},
{"label":"SAMPLE payment fees","total_minor":5000}]}
JSON
2026-09-05 18:38:39 +02:00
# Two paid orders, written straight to the ledger: the catalogue lists no
# priced goods, so checkout cannot make the sales row non-zero and this is
# the only way to fill it. Same event shapes Orders.cpp appends; the slug
# names nothing in the catalogue, so the order page shows it verbatim.
2026-08-14 04:14:13 +02:00
cat > "$WORK/orders.jsonl" <<'JSON'
2026-09-05 18:38:39 +02:00
{"type":"order","at":"2026-08-10T10:00:00Z","id":"1111111111111111aaaaaaaaaaaaaaaa","ref":"CC-111111","product":"sample-goods","color":"green","quantity":1,"unit_minor":56330,"email":"sample@example.org","name":"Sample Buyer","street":"1 Example St","postal":"1000AA","city":"Amsterdam","country":"NL","goods_minor":56330,"shipping_minor":713,"total_minor":57043,"vat_included":true,"status":"awaiting_payment","pay_choice":"bank","pay_url":"","pay_id":"dev-1"}
2026-08-14 04:14:13 +02:00
{"type":"status","at":"2026-08-10T10:04:00Z","id":"1111111111111111aaaaaaaaaaaaaaaa","status":"paid","via":"ideal"}
2026-09-05 18:38:39 +02:00
{"type":"order","at":"2026-08-12T14:30:00Z","id":"2222222222222222bbbbbbbbbbbbbbbb","ref":"CC-222222","product":"sample-goods","color":"white","quantity":1,"unit_minor":65488,"email":"other@example.org","name":"Other Buyer","street":"2 Example Rd","postal":"3000BB","city":"Rotterdam","country":"DE","goods_minor":65488,"shipping_minor":2500,"total_minor":67988,"vat_included":true,"status":"awaiting_payment","pay_choice":"crypto","pay_url":"","pay_id":"dev-2"}
2026-08-14 04:14:13 +02:00
{"type":"status","at":"2026-08-12T14:33:00Z","id":"2222222222222222bbbbbbbbbbbbbbbb","status":"paid","via":"bitcoin"}
JSON
echo "dev: seeded SAMPLE financials (DEV_FINANCIALS=1) — figures are invented"
fi
in_sdk "$PWD/$SRV/catcrafts-server" --serve "$BACKEND_PORT" \
2026-08-05 04:18:37 +02:00
--orders="$WORK/orders.jsonl" --rail="$RAIL" \
--redirect-base="http://localhost:$PORT" >"$WORK/server.log" 2>&1 &
SRV_PID=$!
caddy run --config "$WORK/Caddyfile" --adapter caddyfile >"$WORK/caddy.log" 2>&1 &
CADDY_PID=$!
# Wait for the front door rather than sleeping a fixed amount.
i=0
while [ "$i" -lt 100 ]; do
curl -s -o /dev/null "http://127.0.0.1:$PORT/api/healthz" 2>/dev/null && break
i=$((i + 1)); sleep 0.1
done
if [ "$i" -ge 100 ]; then
echo "dev: did not come up. server log:" >&2; cat "$WORK/server.log" >&2
echo "dev: caddy log:" >&2; cat "$WORK/caddy.log" >&2
exit 1
fi
cat <<EOF
catcrafts.net is running: http://localhost:$PORT
/ home
/shop product list
/shop/donation donate any amount to reach the payment page
2026-08-05 04:18:37 +02:00
/projects the Crafter suite
/posts fediverse posts
/demos demo list; /demos/raytracer loads the wasm
/legal/privacy privacy notice
2026-08-14 04:14:13 +02:00
/financials open financials$(case "${DEV_FINANCIALS:-0}" in
0) printf '%s' " — empty; DEV_FINANCIALS=1 seeds sample figures,
DEV_FINANCIALS=<file> previews a real one";;
1) printf '%s' " (SAMPLE data — invented figures)";;
*) printf '%s' " (real figures from $DEV_FINANCIALS)";; esac)
2026-08-05 04:18:37 +02:00
/feed.xml Atom feed
/media/* mirrored post media (run tools/fetch-media.sh to populate)
2026-08-14 04:14:13 +02:00
Orders from this session go to a temp file and are discarded on exit.$([ "${DEV_FINANCIALS:-0}" = 1 ] && printf '%s' "
Edit the aggregates and just refresh — they are re-read every request:
\$EDITOR $WORK/orders.jsonl.financials.json")
2026-08-05 04:18:37 +02:00
Payment rail: $RAIL$([ "$RAIL" = fake ] && printf '%s' " — simulate a customer paying with:
2026-08-20 20:15:47 +02:00
touch $WORK/orders.jsonl.fake-paid")$([ "$RAIL" = transfer ] && printf '%s' " — the order page shows
the account details a buyer would transfer to. To simulate the money arriving,
append the credit the bank would have reported, using the order's own
reference (the CC-… code on the order page):
tools/dev-credit.sh $WORK/orders.jsonl CC-XXXXXX 57043
The reconciler picks it up within a minute and the order flips to paid.
Reference matching is deliberately forgiving, so try lower case or spaces to
see that they still settle the same order.")
2026-08-05 04:18:37 +02:00
Ctrl-C to stop.
EOF
# Surface backend output as it happens — this is where a render error shows up.
tail -f "$WORK/server.log" &
TAIL_PID=$!
2026-08-05 04:18:37 +02:00
wait "$SRV_PID"