The page is unreachable without nginx, since every route but /healthz needs the X-Receipt-Token header it adds. `serve --local` serves it without a token instead: it only binds to a loopback address and answers only requests whose Host is localhost, 127.0.0.1 or ::1, so another site can't reach it by DNS rebinding. POSTs still need the page header. Co-Authored-By: Claude Opus 5.5 <[email protected]>
4.3 KiB
4.3 KiB
Receipt Maker
CLI that prints images, notes, and QR codes to an Epson TM-T20II thermal receipt printer (Ethernet via USB adapter, or direct USB with --usb).
Layout
receipt.py— the CLI; subcommand percmd_*functionreceipt_tui.py— Textual TUI (tuisubcommand); thin layer over thecmd_*handlers viareceipt.run_action(), which captures stdout/stderr for the log panel and returns anActionResult(rc, output, error kind).textualis an optional dependency ([tui]extra); the CLI works without itreceipt_web.py— FastAPI server (servesubcommand,[web]extra) behind the site's nginx/receipt/over Tailscale; requires theX-Receipt-Tokenheader nginx adds (serve --localdrops it, binding to loopback only and refusing non-localhostHosts), andX-Requested-With: receipton POSTs. POSTs render the job in the request (InvalidJob→ 400), queue the bytes in aPrintQueueand answer 202; one worker thread sends them, and the page long-polls/api/jobs/{id}./api/statusonly tries the printer lock, so it never waits behind a print. Uploads print from memory. Always runs withno_setup(no sudo; the printer must already be reachable). Deployed on stppi, which is online over Wi-Fi and has the printer cabled to itseth0(printer 192.168.192.168, Pi 192.168.192.10 via thereceipt-printernmcli profile)- Scheduling (all run inside
serve;[web]extra):receipt_store.py(SQLite at$RECEIPT_DATA_DIR/receipt.db, default~/.local/share/receipt-maker; additive-only migrations soupdate.shrollbacks still read it),receipt_models.py(pydantic specs: a schedule is trigger + blocks + options),receipt_triggers.py(next fire times, DST-safe, plain-Englishdescribe()),receipt_sources.py(ICS/RSS/JSON fetch + parse),receipt_content.py(blocks →receipt.Blocks,{var}template fill — neverstr.format),receipt_scheduler.py(tick(now)is deterministic; tests drive it with a fake clock). Webhooks arePOST /hook/{id}with a per-schedule secret web/— the page (vanilla ES modules, no build step), served by token-gated routes with relative URLsdeploy/— systemd user units;update.sh(run byreceipt-update.timer) tests the new Gitea commit in a scratch worktree, then fast-forwards the server's clone, restartsreceipt-weband rolls back if/healthzdoesn't answer. Pushing to Giteamasteris the deployconnect_receipt.sh— brings up the printer's network interface (needs sudo); run automatically before any print over Ethernet. The TUI runs it with output captured (PrinterConfig.quiet_setup, set byrun_action()) so it can't corrupt the screentests/— pytest suite; usesFakePrinter+ mockedrequests, never touches hardware or network
Conventions
- Printer/network constants live at the top of
receipt.py; IP/port overridable via--ip/--portorRECEIPT_PRINTER_IP/RECEIPT_PRINTER_PORT - Receipts are lists of
receipt.Block(height, ESC/POS build, preview parts);render_blocks()makes one job of them and refuses one overMAX_PRINT_HEIGHT_DOTS - Every print is
render_*()→ bytes →send(config, payload):render()builds the job on an escposDummy(ESC @ first, cut last), andsend()connects fresh, writes it once and closes. Only the connect is retried; a mid-job drop raisesPrinterDropped(aPrinterUnavailable) and is never replayed, bysend()or by the scheduler's retries.cmd_*handlers take aPrinterConfig, not a printer handle - User errors raise
InvalidJob, printer trouble raisesPrinterUnavailable;main(),run_action()and the web server turn them into messages / status codes - Images are prepared in memory (
prepare_for_print: white-composite alpha → resize to 576px → autocontrast → 1-bit dither); nothing is written except theimg/download cache.MAX_PRINT_HEIGHT_DOTScaps paper per job (tall images shrink, long notes are refused) run_command()dispatches every subcommand excepttui/serve, for bothmain()andrun_action()(via thePRINT_COMMANDSregistry)--no-setup(PrinterConfig.no_setup) skips the connect script everywhere it would run; check it withconfig.needs_setup
Checks
pytest # required
ruff check . # lint (config in pyproject.toml)
Actual printing can only be verified with the physical printer attached.