Read the config with Viper and environment variables, on Go 1.27.4
The config loader uses Viper instead of BurntSushi/toml. Every setting can also come from UBERBRINGER_<SECTION>_<KEY>, which wins over the file, and UBERBRINGER_CONFIG sets the default of --config; set empty, no file is read, so a container can run from the environment alone. Each setting is bound to its own variable, so a variable named after a section changes nothing. The file is parsed with go-toml and checked as written: unknown keys, empty tables and keys that differ only in case are errors, a UTF-8 BOM is skipped and a UTF-16 file is refused. Values are decoded strictly: durations are strings, numbers decimal, flags true or false, and a fraction for a whole number is refused. Errors name the file, the line of a syntax error and the variables that were set, never their values. `uberbringer config get <key>` prints one setting as the daemon would use it. install.sh reads body_limit, bind_ip and port through it, so the config is checked before the service restarts and Caddy proxies to the configured address. The script then waits for the daemon to answer, downloads Caddy's key and source list before replacing them, requires Caddy's own repository, keeps the Caddyfile during dpkg upgrades, detects an existing import loosely, logs Caddy's other errors, drops query strings from the access log and validates the domain and printer address more tightly. go.mod names Go 1.27.4 with no toolchain line, and the workflows check against the go line. The changelog folds the unreleased entries into 0.2.0, which is not tagged yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92dcacb3b9
commit
eacd0bfd27
18 files changed
+1244
-248
No files matched your search
@@ -54,17 +54,22 @@ it set up. It does not upgrade a host set up by the 0.1 install script, which
|
||||
used nginx and acme.sh. Ports 80 and 443 must be free for Caddy.
|
||||
|
||||
The arguments are the server's domain, the email address for the ACME
|
||||
certificate account, and the printer's address. The domain may contain only
|
||||
letters, digits, dots and hyphens. The printer address may also contain
|
||||
colons, for IPv6. The domain's DNS must point at the server, and ports 80 and
|
||||
443 must be reachable, for the certificate to be issued.
|
||||
certificate account, and the printer's address:
|
||||
|
||||
- The domain must be a DNS name with at least one dot, such as
|
||||
`printer.example.com`, made of letters, digits, hyphens and dots; not an IP
|
||||
address. Its DNS must point at the server, and ports 80 and 443 must be
|
||||
reachable, for the certificate to be issued.
|
||||
- The printer address is an IP address or host name without a port; IPv6
|
||||
addresses may contain colons. A port other than 9100 goes in
|
||||
`printer.port` in the config.
|
||||
|
||||
The script does the following:
|
||||
|
||||
- It installs curl and gnupg, then downloads the latest release for the
|
||||
host's architecture and checks it against the release's `checksums.txt`
|
||||
(sha256). If the check fails, nothing else is installed. To install a given
|
||||
release, name its tag:
|
||||
release, v0.2.0 or later, name its tag:
|
||||
`sudo UBERBRINGER_VERSION=v0.2.0 ./install.sh <domain> <acme-email> <printer-address>`.
|
||||
- It installs the binary to `/usr/local/bin/uberbringer` and the systemd unit
|
||||
from the release archive to
|
||||
@@ -72,21 +77,30 @@ The script does the following:
|
||||
dedicated `uberbringer` user.
|
||||
- It writes `/etc/uberbringer/config.toml` (0640, root:uberbringer) with a
|
||||
fresh secret.
|
||||
- It reads the config with the new binary (`uberbringer config get`) before
|
||||
it restarts the service, and stops if the config does not load. After the
|
||||
restart it waits up to 15 seconds for the daemon to answer, and stops if it
|
||||
does not.
|
||||
- It installs [Caddy](https://caddyserver.com) from Caddy's own apt repository
|
||||
as the HTTPS reverse proxy. Caddy obtains and renews the certificate itself
|
||||
and redirects HTTP to HTTPS.
|
||||
as the HTTPS reverse proxy, and stops if that repository can't be read.
|
||||
Caddy obtains and renews the certificate itself and redirects HTTP to
|
||||
HTTPS.
|
||||
- The site is `/etc/caddy/uberbringer.caddy`; `/etc/caddy/Caddyfile`
|
||||
imports it. The package's default Caddyfile is replaced and kept as
|
||||
`Caddyfile.default`.
|
||||
- Caddy passes the client address in `X-Real-IP`, adds HSTS and other
|
||||
security headers, and logs requests to
|
||||
`/var/log/caddy/uberbringer-access.log`.
|
||||
- Caddy proxies to `webserver.bind_ip` and `webserver.port` from the
|
||||
config, passes the client address in `X-Real-IP`, and adds HSTS and other
|
||||
security headers.
|
||||
- Caddy limits request bodies to `webserver.body_limit` from the config
|
||||
(4 MiB by default). It answers a larger body itself, with the same `413`
|
||||
`body_too_large` JSON the daemon sends.
|
||||
`body_too_large` JSON the daemon sends. Its other errors, such as a `502`
|
||||
while the daemon restarts or is down, have an empty body; Caddy logs
|
||||
their cause to `journalctl -u caddy`.
|
||||
- Requests are logged to `/var/log/caddy/uberbringer-access.log`, without
|
||||
query strings.
|
||||
- The configuration is validated as the `caddy` user before it is used. If
|
||||
Caddy rejects it, the previous Caddyfile and site file are put back and
|
||||
the script stops.
|
||||
the script stops with Caddy's error.
|
||||
- It writes the first API token to `/etc/uberbringer/initial-token` (0600,
|
||||
readable by root only) instead of printing it. Copy the token to your
|
||||
client, then delete the file.
|
||||
@@ -101,13 +115,15 @@ edits to that file are lost. It keeps:
|
||||
edit `printer.address` in `/etc/uberbringer/config.toml` instead. The daemon
|
||||
reads the config only when it starts: after an edit, run
|
||||
`systemctl restart uberbringer`. After changing `webserver.body_limit`,
|
||||
re-run the script instead, which copies the new limit into Caddy's site and
|
||||
restarts the service.
|
||||
- **Your Caddyfile edits.** Put global options and your other sites in the
|
||||
Caddyfile. Once you have edited it, a re-run only adds the `import` line if
|
||||
it is missing (keeping the previous copy as `Caddyfile.bak`), and the file
|
||||
keeps its owner and mode. Überbringer's own site can't be changed there,
|
||||
because `uberbringer.caddy` defines it.
|
||||
`bind_ip` or `port`, re-run the script instead, which copies them into
|
||||
Caddy's site and restarts the service.
|
||||
- **Your Caddyfile edits.** Put your other sites in the Caddyfile, and
|
||||
global options at its top, above every import and site. Once you have
|
||||
edited it, a re-run adds the `import` line only if no line imports the
|
||||
site file, by its path or by a glob such as `import /etc/caddy/*.caddy`
|
||||
(keeping the previous copy as `Caddyfile.bak`). The file keeps its owner
|
||||
and mode. Überbringer's own site can't be changed there, because
|
||||
`uberbringer.caddy` defines it.
|
||||
- **The certificate.** Caddy stores it and renews it before it expires.
|
||||
- **Tokens.** A re-run creates none. Issue more with
|
||||
`uberbringer token --config /etc/uberbringer/config.toml --subject <client>`.
|
||||
@@ -125,21 +141,43 @@ The systemd unit ships as
|
||||
|
||||
```sh
|
||||
docker build -t uberbringer .
|
||||
docker run -d -p 8080:8080 -v "$PWD/config.toml:/etc/uberbringer/config.toml:ro" 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. In the mounted `config.toml`:
|
||||
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 your proxy's address or its container network
|
||||
(e.g. `["172.18.0.0/16"]`), and 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.
|
||||
- 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
|
||||
@@ -624,7 +662,25 @@ Copy [configs/config.example.toml](configs/config.example.toml) to
|
||||
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.
|
||||
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:
|
||||
|
||||
@@ -657,9 +713,10 @@ Settings worth knowing:
|
||||
- `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. 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
|
||||
- `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:
|
||||
@@ -683,9 +740,19 @@ 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
|
||||
@@ -698,16 +765,16 @@ printing is finished, and jobs still waiting are marked failed.
|
||||
|
||||
## Development
|
||||
|
||||
The Go version is set in `go.mod`:
|
||||
The Go version is the `go 1.27.4` line in `go.mod`, the latest Go release:
|
||||
|
||||
- The `go 1.26.0` line is the minimum, and builds with `GOTOOLCHAIN=local`
|
||||
need at least that.
|
||||
- The `toolchain go1.27.4` line picks the Go release that CI tests, scans and
|
||||
releases with. With the default `GOTOOLCHAIN=auto`, an older local Go
|
||||
- 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.
|
||||
- To move to a new Go release, change the toolchain line. In the same commit,
|
||||
bump the tool modules golang.org/x/tools, golang.org/x/vuln and
|
||||
honnef.co/go/tools. The workflows read the version from `go.mod`.
|
||||
- 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
|
||||
@@ -742,7 +809,7 @@ module path is `uberbringer`, so packages import as
|
||||
cmd/uberbringer/ main: build info, signals, exit code
|
||||
internal/app/uberbringer/ CLI commands and wiring
|
||||
server/ Fiber HTTP API, auth, health
|
||||
internal/pkg/config/ TOML config: defaults and validation
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user