Compare commits
9 Commits
28de60d375
...
5c932158e9
Author | SHA1 | Date | |
---|---|---|---|
5c932158e9 | |||
1296c9e399 | |||
0f2452eca0 | |||
b6cc98a165 | |||
c5da33f10f | |||
69f4de6739 | |||
649ac2a3c2 | |||
5587fdaaa8 | |||
8f676f08a9 |
66
docs/docs.go
66
docs/docs.go
@ -23,8 +23,47 @@ const docTemplate = `{
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/cards": {
|
||||
"/v1/barcodes/{type}/{content}": {
|
||||
"get": {
|
||||
"description": "Generate barcodes based on the provided data",
|
||||
"produces": [
|
||||
"image/png"
|
||||
],
|
||||
"tags": [
|
||||
"barcodes"
|
||||
],
|
||||
"summary": "Generate barcodes",
|
||||
"parameters": [
|
||||
{
|
||||
"enum": [
|
||||
"ean13",
|
||||
"code128"
|
||||
],
|
||||
"type": "string",
|
||||
"description": "Barcode type",
|
||||
"name": "type",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"minLength": 1,
|
||||
"type": "string",
|
||||
"description": "Barcode content",
|
||||
"name": "content",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/v1/pdfs/cards": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate cards based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -33,7 +72,7 @@ const docTemplate = `{
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"cards"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate runner cards",
|
||||
"parameters": [
|
||||
@ -50,8 +89,13 @@ const docTemplate = `{
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/certificates": {
|
||||
"/v1/pdfs/certificates": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate certificates based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -60,7 +104,7 @@ const docTemplate = `{
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"certificates"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate runner certificates",
|
||||
"parameters": [
|
||||
@ -77,8 +121,13 @@ const docTemplate = `{
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/contracts": {
|
||||
"/v1/pdfs/contracts": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate a contract based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -87,7 +136,7 @@ const docTemplate = `{
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"contracts"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate a contract",
|
||||
"parameters": [
|
||||
@ -335,12 +384,7 @@ const docTemplate = `{
|
||||
"name": "key",
|
||||
"in": "query"
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
// SwaggerInfo holds exported Swagger Info so clients can modify it
|
||||
|
@ -14,8 +14,47 @@
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/cards": {
|
||||
"/v1/barcodes/{type}/{content}": {
|
||||
"get": {
|
||||
"description": "Generate barcodes based on the provided data",
|
||||
"produces": [
|
||||
"image/png"
|
||||
],
|
||||
"tags": [
|
||||
"barcodes"
|
||||
],
|
||||
"summary": "Generate barcodes",
|
||||
"parameters": [
|
||||
{
|
||||
"enum": [
|
||||
"ean13",
|
||||
"code128"
|
||||
],
|
||||
"type": "string",
|
||||
"description": "Barcode type",
|
||||
"name": "type",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"minLength": 1,
|
||||
"type": "string",
|
||||
"description": "Barcode content",
|
||||
"name": "content",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/v1/pdfs/cards": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate cards based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -24,7 +63,7 @@
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"cards"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate runner cards",
|
||||
"parameters": [
|
||||
@ -41,8 +80,13 @@
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/certificates": {
|
||||
"/v1/pdfs/certificates": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate certificates based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -51,7 +95,7 @@
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"certificates"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate runner certificates",
|
||||
"parameters": [
|
||||
@ -68,8 +112,13 @@
|
||||
"responses": {}
|
||||
}
|
||||
},
|
||||
"/contracts": {
|
||||
"/v1/pdfs/contracts": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
],
|
||||
"description": "Generate a contract based on the provided data",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -78,7 +127,7 @@
|
||||
"application/pdf"
|
||||
],
|
||||
"tags": [
|
||||
"contracts"
|
||||
"pdfs"
|
||||
],
|
||||
"summary": "Generate a contract",
|
||||
"parameters": [
|
||||
@ -326,10 +375,5 @@
|
||||
"name": "key",
|
||||
"in": "query"
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ApiKeyAuth": []
|
||||
}
|
||||
]
|
||||
}
|
@ -163,7 +163,31 @@ info:
|
||||
termsOfService: https://lauf-fuer-kaya.de/datenschutz
|
||||
title: LfK Document Server API
|
||||
paths:
|
||||
/cards:
|
||||
/v1/barcodes/{type}/{content}:
|
||||
get:
|
||||
description: Generate barcodes based on the provided data
|
||||
parameters:
|
||||
- description: Barcode type
|
||||
enum:
|
||||
- ean13
|
||||
- code128
|
||||
in: path
|
||||
name: type
|
||||
required: true
|
||||
type: string
|
||||
- description: Barcode content
|
||||
in: path
|
||||
minLength: 1
|
||||
name: content
|
||||
required: true
|
||||
type: string
|
||||
produces:
|
||||
- image/png
|
||||
responses: {}
|
||||
summary: Generate barcodes
|
||||
tags:
|
||||
- barcodes
|
||||
/v1/pdfs/cards:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
@ -178,10 +202,12 @@ paths:
|
||||
produces:
|
||||
- application/pdf
|
||||
responses: {}
|
||||
security:
|
||||
- ApiKeyAuth: []
|
||||
summary: Generate runner cards
|
||||
tags:
|
||||
- cards
|
||||
/certificates:
|
||||
- pdfs
|
||||
/v1/pdfs/certificates:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
@ -196,10 +222,12 @@ paths:
|
||||
produces:
|
||||
- application/pdf
|
||||
responses: {}
|
||||
security:
|
||||
- ApiKeyAuth: []
|
||||
summary: Generate runner certificates
|
||||
tags:
|
||||
- certificates
|
||||
/contracts:
|
||||
- pdfs
|
||||
/v1/pdfs/contracts:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
@ -214,11 +242,11 @@ paths:
|
||||
produces:
|
||||
- application/pdf
|
||||
responses: {}
|
||||
summary: Generate a contract
|
||||
tags:
|
||||
- contracts
|
||||
security:
|
||||
- ApiKeyAuth: []
|
||||
summary: Generate a contract
|
||||
tags:
|
||||
- pdfs
|
||||
securityDefinitions:
|
||||
ApiKeyAuth:
|
||||
in: query
|
||||
|
32
handlers/barcode.go
Normal file
32
handlers/barcode.go
Normal file
@ -0,0 +1,32 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// GenerateBarcode godoc
|
||||
//
|
||||
// @Summary Generate barcodes
|
||||
// @Description Generate barcodes based on the provided data
|
||||
// @Tags barcodes
|
||||
// @Param type path string true "Barcode type" Enums(ean13, code128)
|
||||
// @Param content path string true "Barcode content" MinLength(1)
|
||||
// @Produce image/png
|
||||
// @Router /v1/barcodes/{type}/{content} [get]
|
||||
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
||||
|
||||
// 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())
|
||||
}
|
@ -10,13 +10,15 @@ import (
|
||||
)
|
||||
|
||||
// GenerateCard godoc
|
||||
//
|
||||
// @Summary Generate runner cards
|
||||
// @Description Generate cards based on the provided data
|
||||
// @Tags cards
|
||||
// @Tags pdfs
|
||||
// @Accept json
|
||||
// @Param data body models.CardRequest true "Card data"
|
||||
// @Produce application/pdf
|
||||
// @Router /cards [post]
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /v1/pdfs/cards [post]
|
||||
func (h *DefaultHandler) GenerateCard(c *fiber.Ctx) error {
|
||||
cardRequest := new(models.CardRequest)
|
||||
if err := c.BodyParser(cardRequest); err != nil {
|
||||
@ -30,7 +32,6 @@ func (h *DefaultHandler) GenerateCard(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
generator := services.DefaultTemplater{}
|
||||
templateString, err := services.GetTemplate(cardRequest.Locale, "card")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -38,7 +39,7 @@ func (h *DefaultHandler) GenerateCard(c *fiber.Ctx) error {
|
||||
"error": "Template not found",
|
||||
})
|
||||
}
|
||||
template, err := generator.StringToTemplate(templateString)
|
||||
template, err := h.Templater.StringToTemplate(templateString)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
@ -53,15 +54,15 @@ func (h *DefaultHandler) GenerateCard(c *fiber.Ctx) error {
|
||||
BarcodePrefix: h.Config.CardBarcodePrefix,
|
||||
}
|
||||
|
||||
result, err := generator.Execute(template, genConfig)
|
||||
result, err := h.Templater.Execute(template, genConfig)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
c.Set(fiber.HeaderContentType, "text/html")
|
||||
converter := services.GotenbergConverter{BaseUrl: h.Config.GotenbergBaseUrl}
|
||||
pdf, err := converter.ToPdf(result, "a4", false)
|
||||
|
||||
pdf, err := h.Converter.ToPdf(result, "a4", false)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
|
@ -10,13 +10,15 @@ import (
|
||||
)
|
||||
|
||||
// GenerateCertificate godoc
|
||||
//
|
||||
// @Summary Generate runner certificates
|
||||
// @Description Generate certificates based on the provided data
|
||||
// @Tags certificates
|
||||
// @Tags pdfs
|
||||
// @Accept json
|
||||
// @Param data body models.CertificateRequest true "Certificate data"
|
||||
// @Produce application/pdf
|
||||
// @Router /certificates [post]
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /v1/pdfs/certificates [post]
|
||||
func (h *DefaultHandler) GenerateCertificate(c *fiber.Ctx) error {
|
||||
certificateRequest := new(models.CertificateRequest)
|
||||
if err := c.BodyParser(certificateRequest); err != nil {
|
||||
@ -30,7 +32,6 @@ func (h *DefaultHandler) GenerateCertificate(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
generator := services.DefaultTemplater{}
|
||||
templateString, err := services.GetTemplate(certificateRequest.Locale, "certificate")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -38,7 +39,7 @@ func (h *DefaultHandler) GenerateCertificate(c *fiber.Ctx) error {
|
||||
"error": "Template not found",
|
||||
})
|
||||
}
|
||||
template, err := generator.StringToTemplate(templateString)
|
||||
template, err := h.Templater.StringToTemplate(templateString)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
@ -53,15 +54,15 @@ func (h *DefaultHandler) GenerateCertificate(c *fiber.Ctx) error {
|
||||
Locale: certificateRequest.Locale,
|
||||
}
|
||||
|
||||
result, err := generator.Execute(template, genConfig)
|
||||
result, err := h.Templater.Execute(template, genConfig)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
c.Set(fiber.HeaderContentType, "text/html")
|
||||
converter := services.GotenbergConverter{BaseUrl: h.Config.GotenbergBaseUrl}
|
||||
pdf, err := converter.ToPdf(result, "a4", false)
|
||||
|
||||
pdf, err := h.Converter.ToPdf(result, "a4", false)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
|
@ -10,13 +10,15 @@ import (
|
||||
)
|
||||
|
||||
// GenerateContract godoc
|
||||
//
|
||||
// @Summary Generate a contract
|
||||
// @Description Generate a contract based on the provided data
|
||||
// @Tags contracts
|
||||
// @Tags pdfs
|
||||
// @Accept json
|
||||
// @Param data body models.ContractRequest true "Contract data"
|
||||
// @Produce application/pdf
|
||||
// @Router /contracts [post]
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /v1/pdfs/contracts [post]
|
||||
func (h *DefaultHandler) GenerateContract(c *fiber.Ctx) error {
|
||||
contract := new(models.ContractRequest)
|
||||
if err := c.BodyParser(contract); err != nil {
|
||||
@ -32,7 +34,6 @@ func (h *DefaultHandler) GenerateContract(c *fiber.Ctx) error {
|
||||
|
||||
contract.Runners = repeatRunnerArrayItems(contract.Runners, 2)
|
||||
|
||||
generator := services.DefaultTemplater{}
|
||||
templateString, err := services.GetTemplate(contract.Locale, "contract")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -40,7 +41,7 @@ func (h *DefaultHandler) GenerateContract(c *fiber.Ctx) error {
|
||||
"error": "Template not found",
|
||||
})
|
||||
}
|
||||
template, err := generator.StringToTemplate(templateString)
|
||||
template, err := h.Templater.StringToTemplate(templateString)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
@ -57,15 +58,14 @@ func (h *DefaultHandler) GenerateContract(c *fiber.Ctx) error {
|
||||
BarcodePrefix: h.Config.SponsoringBarcodePrefix,
|
||||
}
|
||||
|
||||
result, err := generator.Execute(template, genConfig)
|
||||
result, err := h.Templater.Execute(template, genConfig)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
converter := services.GotenbergConverter{BaseUrl: h.Config.GotenbergBaseUrl}
|
||||
pdf, err := converter.ToPdf(result, "a5", true)
|
||||
pdf, err := h.Converter.ToPdf(result, "a5", true)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
|
@ -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,12 @@ 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
|
||||
BarcodeService services.BarcodeService
|
||||
Templater services.Templater
|
||||
Converter services.Converter
|
||||
}
|
||||
|
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/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"
|
||||
@ -72,8 +73,16 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
barcodeGenerator := &services.DefaultBarcodeService{}
|
||||
handler := handlers.DefaultHandler{
|
||||
Config: config,
|
||||
BarcodeService: barcodeGenerator,
|
||||
Templater: &services.DefaultTemplater{
|
||||
BarcodeService: barcodeGenerator,
|
||||
},
|
||||
Converter: &services.GotenbergConverter{
|
||||
BaseUrl: config.GotenbergBaseUrl,
|
||||
},
|
||||
}
|
||||
|
||||
// Create a new Fiber instance
|
||||
@ -84,19 +93,19 @@ func main() {
|
||||
// Swagger documentation route
|
||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||
|
||||
// @Security ApiKeyAuth
|
||||
v1 := app.Group("/v1")
|
||||
v1.Use(keyauth.New(keyauth.Config{
|
||||
|
||||
pdfv1 := v1.Group("/pdfs")
|
||||
pdfv1.Use(keyauth.New(keyauth.Config{
|
||||
KeyLookup: "query:key",
|
||||
Validator: validateAPIKey,
|
||||
}))
|
||||
|
||||
v1.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
})
|
||||
v1.Post("/contracts", handler.GenerateContract)
|
||||
v1.Post("/cards", handler.GenerateCard)
|
||||
v1.Post("/certificates", handler.GenerateCertificate)
|
||||
pdfv1.Post("/contracts", handler.GenerateContract)
|
||||
pdfv1.Post("/cards", handler.GenerateCard)
|
||||
pdfv1.Post("/certificates", handler.GenerateCertificate)
|
||||
|
||||
v1.Get("/barcodes/:type/:content", handler.GenerateBarcode)
|
||||
|
||||
app.Use(handler.NotFoundHandler)
|
||||
docs.SwaggerInfo.BasePath = "/"
|
||||
|
69
services/barcode.go
Normal file
69
services/barcode.go
Normal file
@ -0,0 +1,69 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"slices"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/code128"
|
||||
"github.com/boombuler/barcode/ean"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
)
|
||||
|
||||
type BarcodeService interface {
|
||||
GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error)
|
||||
IsTypeSupported(format string) bool
|
||||
}
|
||||
|
||||
type DefaultBarcodeService struct {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
var generatedCode barcode.Barcode
|
||||
var err error
|
||||
|
||||
switch format {
|
||||
case "ean13":
|
||||
generatedCode, err = ean.Encode(content)
|
||||
if err != nil {
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
break
|
||||
case "code128":
|
||||
generatedCode, err = code128.Encode(content)
|
||||
if err != nil {
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
break
|
||||
case "qr":
|
||||
generatedCode, err = qr.Encode(content, qr.M, qr.AlphaNumeric)
|
||||
if err != nil {
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
scaledCode, err := barcode.Scale(generatedCode, width, height)
|
||||
if err != nil {
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = png.Encode(&buf, scaledCode)
|
||||
if err != nil {
|
||||
return bytes.Buffer{}, err
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (b *DefaultBarcodeService) IsTypeSupported(format string) bool {
|
||||
supportedTypes := []string{"ean13", "code128", "qr"}
|
||||
return slices.Contains(supportedTypes, format)
|
||||
}
|
@ -6,13 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"image/png"
|
||||
"strings"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/code128"
|
||||
"github.com/boombuler/barcode/ean"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
)
|
||||
|
||||
type Templater interface {
|
||||
@ -21,6 +15,7 @@ type Templater interface {
|
||||
}
|
||||
|
||||
type DefaultTemplater struct {
|
||||
BarcodeService BarcodeService
|
||||
}
|
||||
|
||||
func idToEan13(id string, prefix string) (string, error) {
|
||||
@ -37,48 +32,18 @@ func idToEan13(id string, prefix string) (string, error) {
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) {
|
||||
var generatedCode barcode.Barcode
|
||||
var err error
|
||||
|
||||
switch format {
|
||||
case "ean13":
|
||||
encodedEan, err := idToEan13(code, prefix)
|
||||
if format == "ean13" {
|
||||
code, err = idToEan13(code, prefix)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
generatedCode, err = ean.Encode(encodedEan)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
break
|
||||
case "code128":
|
||||
generatedCode, err = code128.Encode(prefix + code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
break
|
||||
case "qr":
|
||||
generatedCode, err = qr.Encode(prefix+code, qr.M, qr.AlphaNumeric)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
break
|
||||
default:
|
||||
return "", errors.New("unknown barcode format")
|
||||
}
|
||||
|
||||
scaledCode, err := barcode.Scale(generatedCode, 1000, 500)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
buf, err := t.BarcodeService.GenerateBarcode(format, code, 1000, 500)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = png.Encode(&buf, scaledCode)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
||||
return base64.StdEncoding.EncodeToString(buf.Bytes()), err
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) SelectSponsorImage(id int) (string, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user