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,128 @@
|
||||
package printer_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anomalous.dev/57_Wolve/uberbringer/internal/pkg/escpos"
|
||||
"git.anomalous.dev/57_Wolve/uberbringer/internal/pkg/printer"
|
||||
)
|
||||
|
||||
// fakePrinter accepts one connection per handle call.
|
||||
func fakePrinter(t *testing.T, handle func(net.Conn)) string {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = ln.Close() })
|
||||
go func() {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
handle(conn)
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return ln.Addr().String()
|
||||
}
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
received := make(chan []byte, 1)
|
||||
addr := fakePrinter(t, func(conn net.Conn) {
|
||||
data, _ := io.ReadAll(conn)
|
||||
received <- data
|
||||
})
|
||||
client := printer.NewClient(addr, time.Second, time.Second)
|
||||
if err := client.Send(context.Background(), []byte("receipt")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case got := <-received:
|
||||
if !bytes.Equal(got, []byte("receipt")) {
|
||||
t.Errorf("printer got %q", got)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("printer received nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendUnreachable(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := ln.Addr().String()
|
||||
_ = ln.Close() // nothing listens here now
|
||||
|
||||
client := printer.NewClient(addr, 500*time.Millisecond, time.Second)
|
||||
if err := client.Send(context.Background(), []byte("x")); err == nil {
|
||||
t.Fatal("expected a connection error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendRespectsCancelledContextWhileBusy(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
addr := fakePrinter(t, func(conn net.Conn) {
|
||||
<-release
|
||||
_, _ = io.Copy(io.Discard, conn)
|
||||
})
|
||||
t.Cleanup(func() { close(release) })
|
||||
client := printer.NewClient(addr, time.Second, 5*time.Second)
|
||||
|
||||
// Hold the single connection slot with a status query that never gets
|
||||
// an answer, then try to send with a short deadline.
|
||||
go func() { _, _ = client.Status(context.Background()) }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := client.Send(ctx, []byte("x")); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Errorf("err = %v, want context.DeadlineExceeded", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
answers := map[byte]byte{1: 0x12, 2: 0x12, 3: 0x12, 4: 0x1E} // paper near end
|
||||
addr := fakePrinter(t, func(conn net.Conn) {
|
||||
req := make([]byte, 3)
|
||||
for {
|
||||
if _, err := io.ReadFull(conn, req); err != nil {
|
||||
return
|
||||
}
|
||||
if req[0] != 0x10 || req[1] != 0x04 {
|
||||
return
|
||||
}
|
||||
_, _ = conn.Write([]byte{answers[req[2]]})
|
||||
}
|
||||
})
|
||||
client := printer.NewClient(addr, time.Second, time.Second)
|
||||
status, err := client.Status(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := (escpos.Status{Online: true, PaperNearEnd: true}); status != want {
|
||||
t.Errorf("status = %+v, want %+v", status, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusUnsupported(t *testing.T) {
|
||||
addr := fakePrinter(t, func(conn net.Conn) {
|
||||
_, _ = io.Copy(io.Discard, conn) // a printer that never answers
|
||||
})
|
||||
client := printer.NewClient(addr, time.Second, time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
|
||||
defer cancel()
|
||||
if _, err := client.Status(ctx); !errors.Is(err, printer.ErrStatusUnsupported) {
|
||||
t.Errorf("err = %v, want ErrStatusUnsupported", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user