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>
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package escpos
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/text/encoding/charmap"
|
|
)
|
|
|
|
// CodePage is a printer character table. It ties the ESC t table number the
|
|
// printer understands to the character map used to transcode UTF-8 text, so
|
|
// the two can never disagree.
|
|
type CodePage int
|
|
|
|
// Supported code pages. CodePage437 is the Epson factory default.
|
|
const (
|
|
CodePage437 CodePage = iota + 1
|
|
CodePage850
|
|
CodePage858
|
|
CodePage1252
|
|
)
|
|
|
|
// ParseCodePage returns the code page named by name, e.g. "cp437".
|
|
func ParseCodePage(name string) (CodePage, error) {
|
|
switch strings.ToLower(strings.TrimSpace(name)) {
|
|
case "cp437", "437", "pc437":
|
|
return CodePage437, nil
|
|
case "cp850", "850", "pc850":
|
|
return CodePage850, nil
|
|
case "cp858", "858", "pc858":
|
|
return CodePage858, nil
|
|
case "cp1252", "1252", "wpc1252", "windows-1252":
|
|
return CodePage1252, nil
|
|
}
|
|
return 0, fmt.Errorf("%w: unknown code page %q (want cp437, cp850, cp858 or cp1252)",
|
|
ErrInvalidArgument, name)
|
|
}
|
|
|
|
// String returns the canonical code page name.
|
|
func (cp CodePage) String() string {
|
|
switch cp {
|
|
case CodePage437:
|
|
return "cp437"
|
|
case CodePage850:
|
|
return "cp850"
|
|
case CodePage858:
|
|
return "cp858"
|
|
case CodePage1252:
|
|
return "cp1252"
|
|
}
|
|
return fmt.Sprintf("CodePage(%d)", int(cp))
|
|
}
|
|
|
|
// table returns the ESC t table number for cp.
|
|
func (cp CodePage) table() byte {
|
|
switch cp {
|
|
case CodePage850:
|
|
return 2
|
|
case CodePage858:
|
|
return 19
|
|
case CodePage1252:
|
|
return 16
|
|
}
|
|
return 0 // PC437, also the fallback for an invalid CodePage.
|
|
}
|
|
|
|
func (cp CodePage) charmap() *charmap.Charmap {
|
|
switch cp {
|
|
case CodePage850:
|
|
return charmap.CodePage850
|
|
case CodePage858:
|
|
return charmap.CodePage858
|
|
case CodePage1252:
|
|
return charmap.Windows1252
|
|
}
|
|
return charmap.CodePage437
|
|
}
|
|
|
|
// Encode transcodes UTF-8 text to cp, one byte per rune. Runes the code page
|
|
// cannot represent become '?'. Control characters other than newline and tab
|
|
// are dropped, because the printer would interpret them as commands.
|
|
func (cp CodePage) Encode(s string) []byte {
|
|
cm := cp.charmap()
|
|
out := make([]byte, 0, len(s))
|
|
for _, r := range s {
|
|
switch {
|
|
case r == '\n' || r == '\t':
|
|
out = append(out, byte(r))
|
|
case IsControl(r):
|
|
// Dropped: control bytes would be read as printer commands.
|
|
case r < utf8.RuneSelf:
|
|
out = append(out, byte(r))
|
|
default:
|
|
c, ok := cm.EncodeRune(r)
|
|
if !ok || c < 0x20 || c == 0x7F {
|
|
c = '?'
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// IsControl reports whether r is a control character that [CodePage.Encode]
|
|
// drops. Newline and tab are not reported.
|
|
func IsControl(r rune) bool {
|
|
if r == '\n' || r == '\t' {
|
|
return false
|
|
}
|
|
return r < 0x20 || r == 0x7F || (r >= 0x80 && r < 0xA0) || r == utf8.RuneError
|
|
}
|