diff --git a/handlers/barcode.go b/handlers/barcode.go index 3296550..403bb34 100644 --- a/handlers/barcode.go +++ b/handlers/barcode.go @@ -5,6 +5,7 @@ import ( ) // GenerateBarcode godoc +// // @Summary Generate barcodes // @Description Generate barcodes based on the provided data // @Tags barcodes @@ -13,6 +14,19 @@ import ( // @Produce image/png // @Router /v1/barcodes/{type}/{content} [get] func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error { - c.Set(fiber.HeaderContentType, "text/plain") - return c.Send([]byte("Hello, World!")) + + // Get the type and content from the URL + barcodeType := c.Params("type") + barcodeContent := c.Params("content") + + // Generate the barcode + barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, 1000, 1000) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": err.Error(), + }) + } + + c.Set(fiber.HeaderContentType, "image/png") + return c.Send(barcode.Bytes()) } diff --git a/handlers/handlers.go b/handlers/handlers.go index 00a73d3..c36dc9b 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -2,6 +2,7 @@ package handlers import ( "git.odit.services/lfk/document-server/models" + "git.odit.services/lfk/document-server/services" "github.com/gofiber/fiber/v2" ) @@ -9,8 +10,10 @@ type Handler interface { GenerateCard(*fiber.Ctx) error GenerateContract(*fiber.Ctx) error GenerateCertificate(*fiber.Ctx) error + GenerateBarcode(*fiber.Ctx) error } type DefaultHandler struct { - Config *models.Config + Config *models.Config + BarcodeService services.BarcodeService } diff --git a/main.go b/main.go index ae3ddce..635cf07 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "git.odit.services/lfk/document-server/docs" // Correct import path for docs "git.odit.services/lfk/document-server/handlers" "git.odit.services/lfk/document-server/models" + "git.odit.services/lfk/document-server/services" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/keyauth" "github.com/gofiber/swagger" @@ -55,16 +56,16 @@ func loadEnv() error { return nil } -// @title LfK Document Server API -// @description This is the API documentation for the LfK Document Server - a tool for pdf generation. -// @license.name CC BY-NC-SA 4.0 -// @termsOfService https://lauf-fuer-kaya.de/datenschutz -// @contact.name ODIT.Services UG (haftungsbeschränkt) -// @contact.url https://odit.services -// @contact.email info@odit.services -// @securityDefinitions.apiKey ApiKeyAuth -// @in query -// @name key +// @title LfK Document Server API +// @description This is the API documentation for the LfK Document Server - a tool for pdf generation. +// @license.name CC BY-NC-SA 4.0 +// @termsOfService https://lauf-fuer-kaya.de/datenschutz +// @contact.name ODIT.Services UG (haftungsbeschränkt) +// @contact.url https://odit.services +// @contact.email info@odit.services +// @securityDefinitions.apiKey ApiKeyAuth +// @in query +// @name key func main() { err := loadEnv() @@ -72,8 +73,10 @@ func main() { log.Fatal(err) } + barcodeGenerator := &services.DefaultBarcodeService{} handler := handlers.DefaultHandler{ - Config: config, + Config: config, + BarcodeService: barcodeGenerator, } // Create a new Fiber instance diff --git a/services/barcode.go b/services/barcode.go index 2da7e64..788b259 100644 --- a/services/barcode.go +++ b/services/barcode.go @@ -12,15 +12,15 @@ import ( "github.com/boombuler/barcode/qr" ) -type BarcodeGenerator interface { - GenerateBarcode(format string, content string) (bytes.Buffer, error) +type BarcodeService interface { + GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) IsTypeSupported(format string) bool } -type DefaultBarcodeGenerator struct { +type DefaultBarcodeService struct { } -func (b *DefaultBarcodeGenerator) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) { +func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) { if !b.IsTypeSupported(format) { return bytes.Buffer{}, fmt.Errorf("unsupported barcode type: %s", format) } @@ -63,7 +63,7 @@ func (b *DefaultBarcodeGenerator) GenerateBarcode(format string, content string, return buf, nil } -func (b *DefaultBarcodeGenerator) IsTypeSupported(format string) bool { +func (b *DefaultBarcodeService) IsTypeSupported(format string) bool { supportedTypes := []string{"ean13", "code128", "qr"} return slices.Contains(supportedTypes, format) } diff --git a/services/templater.go b/services/templater.go index 51e0839..293eb40 100644 --- a/services/templater.go +++ b/services/templater.go @@ -31,7 +31,7 @@ func idToEan13(id string, prefix string) (string, error) { } func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) { - gen := &DefaultBarcodeGenerator{} + gen := &DefaultBarcodeService{} var err error if format == "ean13" {