feat(services): Logging
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"log"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
@@ -17,6 +16,7 @@ import (
|
||||
"github.com/boombuler/barcode/ean"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type BarcodeService interface {
|
||||
@@ -26,19 +26,26 @@ type BarcodeService interface {
|
||||
|
||||
type DefaultBarcodeService struct {
|
||||
RedisClient *redis.Client
|
||||
Logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int, padding int) (bytes.Buffer, error) {
|
||||
ctx := context.Background()
|
||||
logger := b.Logger.Named("GenerateBarcode")
|
||||
|
||||
if !b.IsTypeSupported(format) {
|
||||
logger.Errorw("Unsupported barcode type", "type", format)
|
||||
return bytes.Buffer{}, fmt.Errorf("unsupported barcode type: %s", format)
|
||||
}
|
||||
|
||||
logger = logger.With("type", format, "content", content, "width", width, "height", height, "padding", padding)
|
||||
cacheKey := fmt.Sprintf("barcode:%s:%s:%d:%d:%d", format, content, width, height, padding)
|
||||
|
||||
if b.RedisClient != nil {
|
||||
cachedBarcode, err := b.RedisClient.Get(ctx, fmt.Sprintf("barcode:%s:%s:%d:%d:%d", format, content, width, height, padding)).Result()
|
||||
logger.Debugw("Checking cache for barcode", "key", cacheKey)
|
||||
cachedBarcode, err := b.RedisClient.Get(ctx, cacheKey).Result()
|
||||
if err == nil {
|
||||
log.Printf("Cache hit for barcode:%s:%s:%d:%d", format, content, width, height)
|
||||
logger.Infow("Barcode found in cache", "key", cacheKey)
|
||||
buf := bytes.Buffer{}
|
||||
buf.Write([]byte(cachedBarcode))
|
||||
return buf, nil
|
||||
@@ -73,6 +80,7 @@ func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, w
|
||||
bg := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
white := color.RGBA{255, 255, 255, 255}
|
||||
draw.Draw(bg, bg.Bounds(), &image.Uniform{white}, image.Point{}, draw.Src)
|
||||
logger.Debug("Created white background")
|
||||
|
||||
// Calculate the new size for the barcode to fit within the padding
|
||||
newWidth := width - 2*padding
|
||||
@@ -81,24 +89,32 @@ func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, w
|
||||
// Scale the barcode to the new size
|
||||
scaledCode, err := barcode.Scale(generatedCode, newWidth, newHeight)
|
||||
if err != nil {
|
||||
logger.Errorw("Failed to scale barcode", "error", err)
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
logger.Debug("Scaled barcode")
|
||||
|
||||
// Draw the barcode on top of the white background with padding
|
||||
draw.Draw(bg, scaledCode.Bounds().Add(image.Point{padding, padding}), scaledCode, image.Point{}, draw.Over)
|
||||
logger.Debug("Drew barcode on background")
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = png.Encode(&buf, bg)
|
||||
if err != nil {
|
||||
logger.Errorw("Failed to encode barcode to PNG", "error", err)
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
logger.Debug("Encoded barcode to PNG")
|
||||
|
||||
if b.RedisClient != nil {
|
||||
err = b.RedisClient.Set(ctx, fmt.Sprintf("barcode:%s:%s:%d:%d", format, content, width, height), buf.String(), 10*time.Minute).Err()
|
||||
err = b.RedisClient.Set(ctx, cacheKey, buf.String(), 10*time.Minute).Err()
|
||||
logger.Debugw("Cached barcode", "key", cacheKey)
|
||||
if err != nil {
|
||||
logger.Errorw("Failed to cache barcode", "error", err)
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
}
|
||||
logger.Info("Generated barcode")
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user