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