Refactor to Go standards; add print queue and own ESC/POS library
CI / Lint, test and scan (push) Successful in 1m41s
CI / Lint, test and scan (push) Successful in 1m41s
Restructure the daemon to follow the project's Go coding standards and
expand its features.
Layout and tooling
- cmd/uberbringer/main.go is now thin; all logic lives under internal/.
- Module path is git.anomalous.dev/57_Wolve/uberbringer, go 1.26, every
dependency upgraded, vendor/ removed.
- staticcheck, govulncheck and goimports pinned as go tools; add a
Makefile, CI workflow, goreleaser v2 config and Alpine Dockerfile.
Features
- Background print queue with job status (GET /api/v1/jobs/:id), send
retries with backoff, and graceful shutdown.
- New {"elements": [...]} job format on POST /api/v1/print. Breaking:
replaces {"data": [...]}, and the response is now JSON 202.
- In-house ESC/POS library replacing justinmichaelvieira/escpos: sizes
1-8, Font B, CODE39/CODE128, QR, banded raster images, DLE EOT status.
- UTF-8 text transcoded to CP437 (configurable) with word-aware wrapping.
- Images from URL or base64, scaled and dithered; URL fetches refuse
internal addresses (SSRF guard).
- Fiber v3 HTTP server, TypeID (UUIDv7) job and request IDs, /healthz
with live printer status, `secret` and `token` subcommands.
Fixes
- Jobs are validated before anything is sent; a failed printer dial no
longer panics.
- Request logs never contain tokens; Sentry no longer receives the
Authorization header or print contents.
- Rate limiting is per client behind nginx (X-Real-IP).
- install.sh writes the [printer] section, runs the service as a
dedicated user and uses a current nginx TLS config.
config.toml is no longer tracked; start from configs/config.example.toml.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bb73348433
commit
6502d9551d
807 files changed
+6659
-500291
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package escpos
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QRCorrection is the QR code error correction level. Higher levels survive
|
||||
// more damage at the cost of a denser code.
|
||||
type QRCorrection byte
|
||||
|
||||
// QRCorrection values for GS ( k function 169.
|
||||
const (
|
||||
QRCorrectionL QRCorrection = 48 // about 7% recovery
|
||||
QRCorrectionM QRCorrection = 49 // about 15% recovery
|
||||
QRCorrectionQ QRCorrection = 50 // about 25% recovery
|
||||
QRCorrectionH QRCorrection = 51 // about 30% recovery
|
||||
)
|
||||
|
||||
// QR code limits.
|
||||
const (
|
||||
MaxQRData = 7089 // bytes the printer's symbol storage accepts
|
||||
MinQRModuleSize = 1
|
||||
MaxQRModuleSize = 16
|
||||
)
|
||||
|
||||
// ParseQRCorrection returns the correction level named by name: L, M, Q or H.
|
||||
func ParseQRCorrection(name string) (QRCorrection, error) {
|
||||
switch strings.ToUpper(strings.TrimSpace(name)) {
|
||||
case "L":
|
||||
return QRCorrectionL, nil
|
||||
case "M":
|
||||
return QRCorrectionM, nil
|
||||
case "Q":
|
||||
return QRCorrectionQ, nil
|
||||
case "H":
|
||||
return QRCorrectionH, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%w: unknown QR correction level %q (want L, M, Q or H)", ErrInvalidArgument, name)
|
||||
}
|
||||
|
||||
// QRCode appends a model 2 QR code holding data, printed with square modules
|
||||
// moduleSize dots wide.
|
||||
func (b *Builder) QRCode(data []byte, moduleSize int, level QRCorrection) error {
|
||||
switch {
|
||||
case len(data) == 0:
|
||||
return fmt.Errorf("%w: QR data is empty", ErrInvalidArgument)
|
||||
case len(data) > MaxQRData:
|
||||
return fmt.Errorf("%w: QR data is %d bytes, limit is %d", ErrInvalidArgument, len(data), MaxQRData)
|
||||
case moduleSize < MinQRModuleSize || moduleSize > MaxQRModuleSize:
|
||||
return fmt.Errorf("%w: QR module size %d must be within %d-%d",
|
||||
ErrInvalidArgument, moduleSize, MinQRModuleSize, MaxQRModuleSize)
|
||||
case level < QRCorrectionL || level > QRCorrectionH:
|
||||
return fmt.Errorf("%w: unknown QR correction level %d", ErrInvalidArgument, byte(level))
|
||||
}
|
||||
|
||||
// Function 165: select model 2.
|
||||
b.buf.Write([]byte{gs, '(', 'k', 4, 0, '1', 'A', '2', 0})
|
||||
// Function 167: module size.
|
||||
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'C', byte(moduleSize)})
|
||||
// Function 169: error correction level.
|
||||
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'E', byte(level)})
|
||||
// Function 180: store data. The length covers cn, fn, m and the data.
|
||||
n := len(data) + 3
|
||||
b.buf.Write([]byte{gs, '(', 'k', byte(n), byte(n >> 8), '1', 'P', '0'})
|
||||
b.buf.Write(data)
|
||||
// Function 181: print the stored symbol.
|
||||
b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'Q', '0'})
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user