Files
uberbringer/README.md
T
57_WolveandClaude Opus 5.5 bdd337e2dc
Security / Vulnerability check (push) Successful in 38s
Test / Lint and test (push) Successful in 1m53s
Generate the OpenAPI document from a route table
The server registers its routes from one table in server/routes.go, and
tools/genopenapi (run by go generate) writes docs/swagger.json, an
OpenAPI 3.1 document, from the same table and the Go types it names.
Schemas are reflected with invopop/jsonschema: descriptions from the
first paragraph of the doc comments, enums and limits from jsonschema
tags. Request schemas are strict, as the decoder is; response schemas
allow fields added later. The daemon serves the embedded document at
GET /openapi.json without a token, internal/app passes it to the server
in its config, and release archives and the Docker build include it.

Tests keep it honest: a golden test and a CI step fail when the
document is stale, every registered route must be in the table, and the
tags are held to validation, to the Go constants and to the values the
handlers send. receipt.Element and printqueue.Status gain field doc
comments for the descriptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:30:42 -06:00

341 lines
16 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Überbringer
*Bringer of information.* A small daemon that takes print jobs as JSON over a
REST API and prints them on an EPSON ESC/POS network receipt printer.
- **Queue with job status**: jobs return immediately with an ID and print in
order on a background worker, with retries when the printer is offline.
- **Plain-text input**: send UTF-8 text, including accents, `£ ° ±` and
box-drawing `┌─┐│└┘░▒▓`. It is transcoded to the printer's code page
(CP437 by default), and typographic quotes, dashes and the ellipsis
print as ASCII look-alikes where the code page lacks them.
- **Word wrap**: text wraps at word boundaries for the paper width and
character size, so words are never cut in half.
- **Rich elements**: bold, underline, inverse, sizes 1–8, two fonts,
alignment, QR codes, EAN/UPC/CODE39/CODE128 barcodes, images, raw ESC/POS.
- **Boxes, rules and key-value rows**: single or double CP437 borders with
titles, dividers and footers, sized to the paper when the job prints.
- **Preview**: see the exact lines a job prints, without using paper.
- **Images that print well**: PNG, JPEG, GIF or WebP from a URL or base64,
scaled to the paper and dithered. Image URLs can't reach internal network
addresses unless you allow it.
- **Health endpoint** with live printer status: paper low or out, cover open,
errors.
> [!NOTE]
> **Pre-refactor code: no AI was used.** The original Überbringer (v0.1.x),
> up to commit
> [`bb73348`](https://git.anomalous.dev/57_Wolve/uberbringer/src/commit/bb73348433de1604c839de5fb8f8b2793e0e9518)
> from January 2025, was written without any AI. That commit shows the
> code as it was before the 0.2 refactor.
## Contents
- [Quick start](#quick-start)
- [API](#api), in [docs/api.md](docs/api.md)
- [Configuration](#configuration)
- [Command line](#command-line)
- [Development](#development)
- [License](#license)
## Quick start
### Debian/Ubuntu server with Caddy and TLS
```sh
sudo ./install.sh printer.example.com you@example.com 192.168.1.245
```
- Sets up a fresh Debian or Ubuntu host: the latest release, a systemd
service, and [Caddy](https://caddyserver.com) for HTTPS with an automatic
certificate. It does not upgrade a host set up by the 0.1 script.
- Arguments: the server's domain, whose DNS must point at the server, with
ports 80 and 443 free and reachable; the email address for the certificate
account; and the printer's IP address or host name, without a port.
- A given release, v0.2.0 or later:
`sudo UBERBRINGER_VERSION=v0.2.0 ./install.sh <domain> <acme-email> <printer-address>`.
- The config is `/etc/uberbringer/config.toml`. The first API token is in
`/etc/uberbringer/initial-token`: copy it to your client, then delete the
file. Issue more with
`uberbringer token --config /etc/uberbringer/config.toml --subject <client>`.
- Re-running the script upgrades the install and keeps the config. After
editing the config, run `systemctl restart uberbringer`; after changing
`webserver.body_limit`, `bind_ip` or `port`, re-run the script instead.
- Change the systemd unit with `systemctl edit uberbringer`.
- The install log is `/var/log/uberbringer-install.log`.
### Docker
```sh
docker build -t uberbringer .
docker run -d -p 127.0.0.1:8080:8080 -v "$PWD/config.toml:/etc/uberbringer/config.toml:ro" uberbringer
```
The image is a microservice for running behind your own reverse proxy. It
contains no proxy and no TLS: it serves plain HTTP on port 8080, and the proxy
terminates HTTPS. The examples publish the port on the host's loopback
address only, for a proxy on the host; a proxy in another container reaches
it over a shared Docker network instead, with no `-p` at all. In the mounted
`config.toml`:
- Set `bind_ip = "0.0.0.0"` in `[webserver]`.
- Set `trusted_proxies` to the address the proxy's requests come from: its
container network (e.g. `["172.18.0.0/16"]`), or for a proxy on the host,
the Docker bridge's gateway (usually `172.17.0.1`). Have the proxy set
`X-Real-IP` to the client's address. Otherwise every request appears to
come from the proxy, and all clients share one rate limit.
- Let the proxy pass request bodies up to `webserver.body_limit` (4 MiB by
default).
The settings can come from environment variables instead (see
[Configuration](#configuration)). To run without a config file, set
`UBERBRINGER_CONFIG` empty and put the variables in an env file that only you
can read (`chmod 600 uberbringer.env`), so the secret stays off the command
line:
```text
UBERBRINGER_CONFIG=
UBERBRINGER_WEBSERVER_BIND_IP=0.0.0.0
UBERBRINGER_WEBSERVER_TRUSTED_PROXIES=172.18.0.0/16
UBERBRINGER_API_SECRET=<the output of uberbringer secret>
UBERBRINGER_PRINTER_ADDRESS=192.168.1.245
```
```sh
docker run -d -p 127.0.0.1:8080:8080 --env-file uberbringer.env uberbringer
```
Publish port 8080 only to the proxy, never directly to the internet. For
container health checks, use `GET /` to see whether the daemon is up.
`/healthz` answers `503` while the printer is unreachable, so it suits
alerting better than restarting the container.
### From source
```sh
cp configs/config.example.toml config.toml
go run ./cmd/uberbringer secret # paste into api.secret
$EDITOR config.toml # set printer.address
go run ./cmd/uberbringer token # prints a bearer token
make run
```
## API
[docs/api.md](docs/api.md) is the API reference: the routes, the print
elements and blocks, and the error codes.
[docs/swagger.json](docs/swagger.json) is the OpenAPI 3.1 document,
generated from the route table; the daemon serves it at `GET /openapi.json`.
## Configuration
Copy [configs/config.example.toml](configs/config.example.toml) to
`config.toml`. Only `api.secret` (at least 32 bytes) and `printer.address` are
required; every other setting has a default, which the example shows. Configs
from 0.1 still load, because their keys are unchanged, but `printer.address`
is now required. Unknown keys are reported as errors, so a typo can't
silently fall back to a default, and so are two keys that differ only in
case. A duration is a string such as `"5s"`; a bare number is refused.
Every key can also be set by an environment variable named
`UBERBRINGER_<SECTION>_<KEY>`, such as `UBERBRINGER_PRINTER_ADDRESS` for
`printer.address`, and a variable wins over the file:
- Values are written without quotes: decimal digits for a number, `true` or
`false` for a flag, `5s` for a duration. Lists take comma-separated values,
e.g. `UBERBRINGER_WEBSERVER_TRUSTED_PROXIES=127.0.0.1,::1`; an empty item,
from a doubled or trailing comma, is an error. In the file, a number, flag
or list may also be written as such a string.
- An empty variable counts as unset, and one that holds only spaces is an
error. Variables that name no key are ignored, but when the configuration
fails to load, the error names them, since they are likely misspelt.
- `--config` names the file, and defaults to `config.toml` or to
`UBERBRINGER_CONFIG` when that is set. `--config ""`, or an empty
`UBERBRINGER_CONFIG`, reads no file, so the settings come from the
environment and the defaults alone.
Settings worth knowing:
- `printer.model` sets the print width and characters per line:
| Model | Printers | Width | Font A / B columns |
|-------|----------|-------|--------------------|
| `tm-t88` (default) | TM-T88IV, V, VI, VII | 512 dots (180 dpi) | 42 / 56 |
| `tm-m50` | TM-m50, TM-m50II | 512 dots (180 dpi) | 42 / 56 |
| `tm-t20` | TM-T20, II, III | 576 dots (203 dpi) | 48 / 64 |
| `generic-80mm` | other 80 mm, 203 dpi | 576 dots | 48 / 64 |
| `generic-58mm` | 58 mm paper, 203 dpi | 384 dots | 32 / 42 |
For 58 mm paper on a 180-dpi printer (TM-T88VII, TM-m50II), use a
`tm-t88` or `tm-m50` model with `paper_width = 360` (30 Font A columns).
If words break at the right edge (for example `seve` on one line and `ral`
on the next), the model is wider than the printer. `paper_width` (dots) and
`columns` (Font A characters per line) override the model.
- `printer.codepage`: `cp437` (default), `cp850`, `cp858` (has €) or
`cp1252`.
- `printer.box_line_spacing` (default `0`): the line spacing, in motion
units (ESC 3, 0–255), used while a box prints. Lines of box drawing leave
small gaps in the vertical borders at the printer's default spacing; a
value checked on paper closes them. `0` keeps the default, which was
checked on a TM-T88IV with `examples/blocks.json` and looks fine.
- `format.max_lines` (default `2000`): a job that would print more lines,
all copies counted, fails instead of printing. It is a backstop against
runaway text; `/api/v1/preview` answers such a job with `422`.
- `webserver.trusted_proxies` / `proxy_header`: the real client IP for
per-client rate limits comes from the `X-Real-IP` header set by the reverse
proxy (Caddy, as `install.sh` configures it) on `127.0.0.1`.
- `sentry.dsn`: error reporting. When neither the file nor
`UBERBRINGER_SENTRY_DSN` sets it, it falls back to the `SENTRY_DSN`
environment variable, then to a DSN built into release binaries (from the
`SENTRY_DSN` CI secret). Request bodies and the `Authorization` header are never sent
to Sentry.
- `log.level` (`debug`, `info`, `warn`, `error`) and `log.format` (`text` or
`json`) control what is logged:
- Each request is logged once, as `request`: at INFO, or at WARN when the
status is 5xx. Only the path is logged, never the query string, the body
or the token.
- An unexpected failure adds one ERROR `request failed` line, with `err` and
`request_id`, and one Sentry event.
- A recovered panic adds one ERROR `panic` line, with `panic` and `stack`,
and one Sentry event.
- A print job that fails after its retries is logged as ERROR `job failed`
with its `job_id`, and is reported to Sentry.
- Refusals are never logged at ERROR and never go to Sentry. Refusals are
4xx answers, the `queue_full` and `shutting_down` 503s, and 503s from
`/healthz`.
## Command line
```text
uberbringer [--config config.toml] run the daemon (default command)
uberbringer secret print a new random API secret
uberbringer token [--subject s] [--ttl 720h] print a signed bearer token
uberbringer preview [file.json|-] show the lines a job would print
uberbringer config get <key> print one setting, e.g. webserver.port
uberbringer version print the version
```
`config get` prints a setting as the daemon would use it, from the file,
`UBERBRINGER_*` variables and the defaults, and fails when the configuration
does not load. Durations print as `15s`, lists comma-separated. It never
prints `api.secret`.
Every command that reads the config takes `--config` (`-c`); see
[Configuration](#configuration) for its default and for running without a
file.
`preview` reads a job from the file, or from standard input with `-` or no
argument, and lays it out for the printer in the config, as
`/api/v1/preview` does. It prints nothing and fetches no images. On a
terminal it shows bold, underlined and inverted text as such; warnings go
to standard error. It reads the same config as the daemon, so
`api.secret` and `printer.address` must be set even though neither is used.
The daemon shuts down gracefully on SIGINT/SIGTERM. A receipt that is already
printing is finished, and jobs still waiting are marked failed.
## Development
The Go version is the `go 1.27.4` line in `go.mod`, the latest Go release:
- It is the minimum for building, and builds with `GOTOOLCHAIN=local` need at
least that. With the default `GOTOOLCHAIN=auto`, an older local Go
downloads go1.27.4 by itself.
- CI tests, scans and releases with exactly that release; the workflows read
it from `go.mod`. There is no separate `toolchain` line.
- To move to a new Go release, change the go line
(`go mod edit -go=1.27.5`). In the same commit, bump the tool modules
golang.org/x/tools, golang.org/x/vuln and honnef.co/go/tools.
```sh
make check # the CI gate, below
make build # bin/uberbringer
make fmt # gofmt + goimports
make vuln # govulncheck
make openapi # regenerate docs/swagger.json (go generate ./...)
make snapshot # release archives in dist/; publishes nothing
```
`make check` runs the same steps as `.gitea/workflows/test.yml`, in the same
order:
1. `fmt-check`: gofmt and goimports.
2. `tidy-check`: `go mod tidy -diff`.
3. `go vet`.
4. staticcheck.
5. `openapi-check`: `go generate ./...` must leave `docs/swagger.json`
unchanged.
6. `go test -race -timeout 5m`.
7. `release-check`: `goreleaser check`, which validates the release config
without building.
It needs goreleaser installed. govulncheck is not part of it; run
`make vuln`. `make snapshot` builds the release archives locally; run it
after changing `.goreleaser.yaml` or `packaging/`. `make` builds report the
version without the leading `v`, e.g. `0.1.2-3-gabc1234`, as release
binaries do.
`docs/swagger.json` is generated by `tools/genopenapi` from the route table
in `internal/app/uberbringer/server/routes.go`, which the server also
registers its routes from. The schemas come from the Go types the routes
name: their `json` and `jsonschema` tags and their doc comments. Never edit
the file by hand: change the route table or the types, run
`go generate ./...`, and commit the document with the change.
The linters are pinned as Go tools in `go.mod`, so `go tool staticcheck`,
`go tool govulncheck` and `go tool goimports` need no separate install. The
module path is `uberbringer`, so packages import as
`uberbringer/internal/...`. Build from a clone of the repository.
```text
cmd/uberbringer/ main: build info, signals, exit code
internal/app/uberbringer/ CLI commands and wiring
server/ Fiber HTTP API, auth, health
internal/pkg/config/ Config from TOML and UBERBRINGER_* variables (Viper): defaults, validation
internal/pkg/escpos/ ESC/POS command builder, code pages, barcodes, QR, raster, status, interpreter
internal/pkg/receipt/ Job format, validation, text folding, word wrap, blocks, rendering, preview
internal/pkg/raster/ Image decoding, scaling and dithering
internal/pkg/safehttp/ SSRF-guarded image fetching
internal/pkg/printer/ TCP printer client (port 9100) and status queries
internal/pkg/printqueue/ Job queue, worker and status history
configs/ Example configuration
examples/ Example jobs, such as the block calibration receipt
packaging/ systemd unit
tools/genopenapi/ Generates docs/swagger.json from the route table
docs/ API reference, OpenAPI document, design notes and feature plans
```
[docs/design.md](docs/design.md) explains why the daemon is shaped as it is.
[CHANGELOG.md](CHANGELOG.md) lists the changes in each release.
### CI and releases
CI runs on the Gitea runner from `.gitea/workflows/`. The repository has no
`.github/` directory.
- `test.yml` runs the `make check` gate on every branch push. It lints,
checks that `docs/swagger.json` is up to date, tests and validates the
release config, but builds no binaries, and it does not run on pull
requests or tags. Binaries are built only by `release.yml`.
- `security.yml` runs govulncheck on pushes to main and every Monday at
06:00 UTC. Scheduled runs use main's copy of the workflow.
- `release.yml` builds and publishes a release with goreleaser 2.20.3 when a
`v*` tag is pushed.
- It runs only for tags whose commit is on main, and refuses any other tag.
- `vX.Y.Z-rc.N` tags are published as prereleases, so they never become the
latest release that `install.sh` installs.
- Each archive holds the binary, `LICENSE.txt`, `README.md`,
`docs/api.md`, `docs/swagger.json`, `CHANGELOG.md`,
`configs/config.example.toml` and `packaging/uberbringer.service`.
- The release notes link to `CHANGELOG.md` at the tag.
## License
Copyright © 2025-2026 William Gill.
Licensed under the European Union Public Licence v. 1.2 (EUPL-1.2). The full
text is in [LICENSE.txt](LICENSE.txt), and translations into every official EU
language are equally valid. Releases up to and including 0.1.2 were published
under the GNU Affero General Public License v3.0.