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