Add a web server for printing from the site, deployed on stppi
`./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:
@@ -14,6 +14,7 @@ The CLI runs the connect script automatically before printing (unless `--usb` is
|
||||
- Python 3.10+
|
||||
- `python-escpos`, `Pillow`, `requests` (see `pyproject.toml`)
|
||||
- `textual` (optional, only for the `tui` subcommand; `pip install '.[tui]'`)
|
||||
- `fastapi`, `uvicorn`, `python-multipart` (optional, only for the `serve` subcommand; `pip install '.[web]'`)
|
||||
- Epson TM-T20II connected via a USB-to-Ethernet adapter (or directly over USB with `--usb`)
|
||||
|
||||
Install the dependencies (or `pip install .` to also get a `receipt` command on your PATH):
|
||||
@@ -64,6 +65,65 @@ fortune | ./receipt.py note
|
||||
|
||||
Images are prepared for thermal output automatically: transparency is composited onto white, then the image is resized to full width, autocontrast-stretched, and Floyd-Steinberg dithered.
|
||||
|
||||
## Printing from the website
|
||||
|
||||
`./receipt.py serve` runs a small web server (`receipt_web.py`, needs `pip install '.[web]'`) with a page for printing a note or an uploaded image. It runs on stppi (the Raspberry Pi). The printer is on the home Wi-Fi at a fixed `192.168.178.250`, so the Pi reaches it over the LAN. The site's nginx on stpvm gates `https://www.adam-french.co.uk/receipt/` on the admin login, then forwards over Tailscale to this server and adds an `X-Receipt-Token` header. The server answers nothing but `/healthz` without that token.
|
||||
|
||||
```
|
||||
browser ─https─> nginx (stpvm, admin auth_request) ─Tailscale─> receipt serve (stppi:8765) ─LAN─> printer
|
||||
```
|
||||
|
||||
`serve` never runs the sudo connect script (`--no-setup` does the same for other commands), so the printer must already be reachable. On Wi-Fi it is. A printer cabled to the server's own Ethernet port instead needs a permanent address on that port:
|
||||
|
||||
```bash
|
||||
sudo nmcli con add type ethernet ifname <port> con-name receipt-printer \
|
||||
ipv4.method manual ipv4.addresses 192.168.192.10/24 ipv4.never-default yes ipv6.method disabled
|
||||
```
|
||||
|
||||
### Setting up the server machine
|
||||
|
||||
1. Clone from Gitea to `~/Coding/receipt_maker` (the path the units use), and install into a venv there:
|
||||
|
||||
```bash
|
||||
git clone ssh://[email protected]:2222/adamf/receipt_maker.git ~/Coding/receipt_maker
|
||||
cd ~/Coding/receipt_maker
|
||||
python3 -m venv .venv && .venv/bin/pip install '.[web,dev]'
|
||||
```
|
||||
|
||||
2. Create the settings file. Put the same token in the web_server `.env` as `RECEIPT_WEB_TOKEN`:
|
||||
|
||||
```bash
|
||||
(umask 077; printf 'RECEIPT_WEB_TOKEN=%s\nRECEIPT_WEB_LISTEN=%s:8765\nRECEIPT_PRINTER_IP=192.168.178.250\n' \
|
||||
"$(openssl rand -hex 32)" "$(tailscale ip -4)" > ~/.config/receipt-web.env)
|
||||
```
|
||||
|
||||
3. Install the units, point the server at the venv, and start everything at boot:
|
||||
|
||||
```bash
|
||||
cp deploy/receipt-web.service deploy/receipt-update.{service,timer} ~/.config/systemd/user/
|
||||
mkdir -p ~/.config/systemd/user/receipt-web.service.d
|
||||
printf '[Service]\nExecStart=\nExecStart=%%h/Coding/receipt_maker/.venv/bin/python %%h/Coding/receipt_maker/receipt.py serve\n' \
|
||||
> ~/.config/systemd/user/receipt-web.service.d/venv.conf
|
||||
systemctl --user daemon-reload
|
||||
systemctl --user enable --now receipt-web receipt-update.timer
|
||||
sudo loginctl enable-linger "$USER"
|
||||
```
|
||||
|
||||
4. On stpvm, set `RECEIPT_WEB_UPSTREAM=<tailscale ip>:8765` in web_server's `.env`, then `docker compose up -d nginx`.
|
||||
|
||||
Jobs run one at a time. If the server machine is off, the site shows "printer machine offline".
|
||||
|
||||
### Automatic updates
|
||||
|
||||
`receipt-update.timer` runs `deploy/update.sh` every 5 minutes. When `master` on Gitea has moved, the script:
|
||||
|
||||
1. fast-forwards the clone
|
||||
2. reinstalls the dependencies if `pyproject.toml` changed
|
||||
3. runs the tests
|
||||
4. restarts `receipt-web`
|
||||
|
||||
If the tests fail, the clone goes back to the previous commit, the old code keeps serving, and that commit is skipped until a newer one is pushed. Its output is in `journalctl --user -u receipt-update`. Pushing to Gitea is the whole deploy.
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
@@ -78,6 +138,9 @@ Both checks also run in CI on every push and pull request (`.github/workflows/ci
|
||||
|
||||
- `receipt.py` — main script
|
||||
- `receipt_tui.py` — Textual terminal UI (`./receipt.py tui`)
|
||||
- `receipt_web.py` — web server for printing from the site (`./receipt.py serve`)
|
||||
- `deploy/receipt-web.service` — systemd user unit for the web server
|
||||
- `deploy/update.sh`, `deploy/receipt-update.{service,timer}` — automatic updates from Gitea
|
||||
- `connect_receipt.sh` — network setup for the USB Ethernet adapter
|
||||
- `tests/` — pytest suite for the CLI and TUI (no printer or network needed)
|
||||
- `tests/` — pytest suite for the CLI, TUI and web server (no printer or network needed)
|
||||
- `img/` — downloaded images cache (gitignored; clear with `./receipt.py clean`)
|
||||
|
||||
Reference in New Issue
Block a user