feat(handlers): Implement barcode generation
This commit is contained in:
parent
b6cc98a165
commit
0f2452eca0
@ -5,6 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GenerateBarcode godoc
|
// GenerateBarcode godoc
|
||||||
|
//
|
||||||
// @Summary Generate barcodes
|
// @Summary Generate barcodes
|
||||||
// @Description Generate barcodes based on the provided data
|
// @Description Generate barcodes based on the provided data
|
||||||
// @Tags barcodes
|
// @Tags barcodes
|
||||||
@ -13,6 +14,19 @@ import (
|
|||||||
// @Produce image/png
|
// @Produce image/png
|
||||||
// @Router /v1/barcodes/{type}/{content} [get]
|
// @Router /v1/barcodes/{type}/{content} [get]
|
||||||
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
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())
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.odit.services/lfk/document-server/models"
|
"git.odit.services/lfk/document-server/models"
|
||||||
|
"git.odit.services/lfk/document-server/services"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,8 +10,10 @@ type Handler interface {
|
|||||||
GenerateCard(*fiber.Ctx) error
|
GenerateCard(*fiber.Ctx) error
|
||||||
GenerateContract(*fiber.Ctx) error
|
GenerateContract(*fiber.Ctx) error
|
||||||
GenerateCertificate(*fiber.Ctx) error
|
GenerateCertificate(*fiber.Ctx) error
|
||||||
|
GenerateBarcode(*fiber.Ctx) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultHandler struct {
|
type DefaultHandler struct {
|
||||||
Config *models.Config
|
Config *models.Config
|
||||||
|
BarcodeService services.BarcodeService
|
||||||
}
|
}
|
||||||
|
25
main.go
25
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/docs" // Correct import path for docs
|
||||||
"git.odit.services/lfk/document-server/handlers"
|
"git.odit.services/lfk/document-server/handlers"
|
||||||
"git.odit.services/lfk/document-server/models"
|
"git.odit.services/lfk/document-server/models"
|
||||||
|
"git.odit.services/lfk/document-server/services"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
||||||
"github.com/gofiber/swagger"
|
"github.com/gofiber/swagger"
|
||||||
@ -55,16 +56,16 @@ func loadEnv() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// @title LfK Document Server API
|
// @title LfK Document Server API
|
||||||
// @description This is the API documentation for the LfK Document Server - a tool for pdf generation.
|
// @description This is the API documentation for the LfK Document Server - a tool for pdf generation.
|
||||||
// @license.name CC BY-NC-SA 4.0
|
// @license.name CC BY-NC-SA 4.0
|
||||||
// @termsOfService https://lauf-fuer-kaya.de/datenschutz
|
// @termsOfService https://lauf-fuer-kaya.de/datenschutz
|
||||||
// @contact.name ODIT.Services UG (haftungsbeschränkt)
|
// @contact.name ODIT.Services UG (haftungsbeschränkt)
|
||||||
// @contact.url https://odit.services
|
// @contact.url https://odit.services
|
||||||
// @contact.email info@odit.services
|
// @contact.email info@odit.services
|
||||||
// @securityDefinitions.apiKey ApiKeyAuth
|
// @securityDefinitions.apiKey ApiKeyAuth
|
||||||
// @in query
|
// @in query
|
||||||
// @name key
|
// @name key
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
err := loadEnv()
|
err := loadEnv()
|
||||||
@ -72,8 +73,10 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
barcodeGenerator := &services.DefaultBarcodeService{}
|
||||||
handler := handlers.DefaultHandler{
|
handler := handlers.DefaultHandler{
|
||||||
Config: config,
|
Config: config,
|
||||||
|
BarcodeService: barcodeGenerator,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Fiber instance
|
// Create a new Fiber instance
|
||||||
|
@ -12,15 +12,15 @@ import (
|
|||||||
"github.com/boombuler/barcode/qr"
|
"github.com/boombuler/barcode/qr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BarcodeGenerator interface {
|
type BarcodeService interface {
|
||||||
GenerateBarcode(format string, content string) (bytes.Buffer, error)
|
GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error)
|
||||||
IsTypeSupported(format string) bool
|
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) {
|
if !b.IsTypeSupported(format) {
|
||||||
return bytes.Buffer{}, fmt.Errorf("unsupported barcode type: %s", 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
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *DefaultBarcodeGenerator) IsTypeSupported(format string) bool {
|
func (b *DefaultBarcodeService) IsTypeSupported(format string) bool {
|
||||||
supportedTypes := []string{"ean13", "code128", "qr"}
|
supportedTypes := []string{"ean13", "code128", "qr"}
|
||||||
return slices.Contains(supportedTypes, format)
|
return slices.Contains(supportedTypes, format)
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ func idToEan13(id string, prefix string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) {
|
func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) {
|
||||||
gen := &DefaultBarcodeGenerator{}
|
gen := &DefaultBarcodeService{}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if format == "ean13" {
|
if format == "ean13" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user