Begin implementation of formatted barcodes

This commit is contained in:
Fred Boniface 2023-09-02 22:05:46 +01:00
parent d3bd2673e8
commit 4dad1406eb
9 changed files with 87 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.png
# Compiled binaries
barcodes
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore

1
formatting/format.go Normal file
View File

@ -0,0 +1 @@
package formatting

8
formatting/vcard.go Normal file
View File

@ -0,0 +1,8 @@
package formatting
type Vcard struct {
FirstName string
Surname string
Mobile string
Home string
}

View File

@ -1,4 +1,4 @@
package strings
package formatting
import "fmt"

56
web/format.route.go Normal file
View File

@ -0,0 +1,56 @@
package web
import (
"encoding/json"
"net/http"
)
func generateFormattedBarcode(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Use a POST request for barcode generation", http.StatusMethodNotAllowed)
return
}
var req FormattedBarcodeRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Define a new type in `formatting` and create an instance of it here
// Call the format function providing the above type
// Create a generation.Parameters struct here using the string obtained from `formatting`
// Call the validate function providing the generation.Parameters var
/* Generate the barcode as per generate.route.go
barcodeGen, err := generation.Generate(parameters)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if barcodeGen == nil {
fmt.Println("Generation failed, no barcode created")
http.Error(w, "Barcode Generation Failed", http.StatusInternalServerError)
return
}
barcodeGen, _ = barcode.Scale(barcodeGen, int(req.Width), int(req.Height))
image, isImage := barcodeGen.(image.Image)
if !isImage {
fmt.Println("Generation failed - no image")
http.Error(w, "Generated Barcode is not an image", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/png")
err = png.Encode(w, image)
if err != nil {
http.Error(w, "Error streaming barcode", http.StatusInternalServerError)
}*/
}

View File

@ -12,7 +12,13 @@ import (
"github.com/boombuler/barcode"
)
// Generates a barcode from the data and options provided in the request body
func generateBarcode(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Use a POST request for barcode generation", http.StatusMethodNotAllowed)
return
}
var req BarcodeRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {

View File

@ -7,7 +7,7 @@ import (
"git.fjla.uk/fred.boniface/barcodes/validation"
)
func buildPage(w http.ResponseWriter, r *http.Request) {
func helpTemplate(w http.ResponseWriter, r *http.Request) {
i := 0
barcodeOptions := make([]string, len(validation.FormatRules))
for k := range validation.FormatRules {

View File

@ -10,10 +10,12 @@ func StartServer() {
fmt.Fprintf(w, "Barcodes")
})
http.HandleFunc("/help", buildPage)
http.HandleFunc("/help", helpTemplate)
http.HandleFunc("/generate", generateBarcode)
http.HandleFunc("/generate/formatted", generateFormattedBarcode)
port := ":8500"
fmt.Printf("Server listening on port %s\n", port)

View File

@ -7,3 +7,12 @@ type BarcodeRequest struct {
ECCLevel int `json:"ecc_level"`
Content string `json:"content"`
}
type FormattedBarcodeRequest struct {
BarcodeType string `json:"barcode_type"`
Width int `json:"width"`
Height int `json:"height"`
ECCLevel int `json:"ecc_level"`
Format string `json:"format"`
Content interface{} `json:"content"`
}