Add a web server for printing from the site, deployed on stppi
CI / test (3.12) (push) Failing after 43s
CI / test (3.10) (push) Failing after 6m6s
CI / test (3.13) (push) Failing after 36s

`./receipt.py serve` (receipt_web.py, the new [web] extra) serves a page
for printing a note or an uploaded image. The site's nginx gates
/receipt/ on the admin session and proxies to it over Tailscale, adding
X-Receipt-Token; nothing but /healthz answers without it, and POSTs also
need X-Requested-With: receipt. Jobs run one at a time behind a lock
around run_action(), and uploads print from memory.

run_action(), _dispatch() and setup_network_quiet() move from the TUI to
receipt.py so the server doesn't need textual. --no-setup (needs_setup())
skips the sudo connect script everywhere it would run; serve always sets
it. cmd_print takes in-memory `data`.

deploy/ has the systemd user units. receipt-update.timer runs update.sh
every 5 minutes: it fast-forwards the clone from Gitea, runs the tests
and restarts receipt-web, rolling back if they fail.

Also included from earlier uncommitted work: the CI matrix moved to the
right place, connect_receipt.sh no longer re-parses CLI values in
bash -c, RECEIPT_PRINTER_IP/PORT are validated, pyusb is declared, the
ruff S rules are on, and the img/ cache and junk/ are no longer tracked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 21:22:31 +01:00
co-authored by Claude Opus 5.5
parent e333d6729f
commit f55b20fb82
65 changed files with 1142 additions and 124712 deletions
+10
View File
@@ -0,0 +1,10 @@
# Pulls new commits for the receipt printer web server (see deploy/update.sh).
# Installed alongside receipt-web.service; started by receipt-update.timer.
[Unit]
Description=Update the receipt printer web server from Gitea
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=%h/Coding/receipt_maker/deploy/update.sh
+13
View File
@@ -0,0 +1,13 @@
# Checks Gitea for receipt_maker updates every 5 minutes.
# cp deploy/receipt-update.{service,timer} ~/.config/systemd/user/
# systemctl --user daemon-reload && systemctl --user enable --now receipt-update.timer
[Unit]
Description=Check for receipt printer web server updates
[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
RandomizedDelaySec=30
[Install]
WantedBy=timers.target
+24
View File
@@ -0,0 +1,24 @@
# Web server for printing from the site (see README "Printing from the website").
#
# Install as a user unit on the machine the printer is cabled to:
# cp deploy/receipt-web.service ~/.config/systemd/user/
# systemctl --user daemon-reload && systemctl --user enable --now receipt-web
# sudo loginctl enable-linger "$USER" # start at boot, without logging in
#
# ~/.config/receipt-web.env holds RECEIPT_WEB_TOKEN=<secret> (chmod 600); the
# site's nginx sends the same value. RECEIPT_WEB_LISTEN overrides the default
# bind address (this machine's Tailscale IP).
[Unit]
Description=Receipt printer web server
After=network-online.target
Wants=network-online.target
[Service]
EnvironmentFile=%h/.config/receipt-web.env
ExecStart=/usr/bin/python3 %h/Coding/receipt_maker/receipt.py serve
# Binding fails until Tailscale has brought its address up; just retry.
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
+36
View File
@@ -0,0 +1,36 @@
#!/bin/bash
# Fast-forward this clone to its upstream (Gitea) and restart the web server
# when the code changed. Run every few minutes by receipt-update.timer on the
# machine the service runs on.
#
# The tests gate the restart: if they fail on the new commit, the clone goes
# back to the old one, the service keeps running the old code, and that
# commit is skipped until a newer one lands.
set -euo pipefail
cd "$(dirname "$0")/.."
git fetch --quiet
old=$(git rev-parse HEAD)
new=$(git rev-parse '@{upstream}')
[ "$old" = "$new" ] && exit 0
failed_marker="$(git rev-parse --git-dir)/receipt-update-failed"
if [ "$(cat "$failed_marker" 2>/dev/null)" = "$new" ]; then
exit 0
fi
git merge --ff-only --quiet "$new"
if ! git diff --quiet "$old" "$new" -- pyproject.toml; then
.venv/bin/pip install --quiet '.[web,dev]'
fi
if ! output=$(.venv/bin/python -m pytest -q -p no:cacheprovider 2>&1); then
echo "$output" | tail -n 20
echo "Tests failed on $(git rev-parse --short "$new"); staying on $(git rev-parse --short "$old")." >&2
git reset --quiet --hard "$old"
echo "$new" >"$failed_marker"
exit 1
fi
systemctl --user restart receipt-web
echo "Updated $(git rev-parse --short "$old") -> $(git rev-parse --short "$new")."