From 4dad1406eb84145186d34b000828181fae6b86fa Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 2 Sep 2023 22:05:46 +0100 Subject: [PATCH] Begin implementation of formatted barcodes --- .gitignore | 3 +- formatting/format.go | 1 + formatting/vcard.go | 8 +++ {strings => formatting}/wifi.go | 2 +- web/format.route.go | 56 +++++++++++++++++++++ web/{routeGenerate.go => generate.route.go} | 6 +++ web/{help.go => help.route.go} | 2 +- web/server.go | 4 +- web/types.go | 9 ++++ 9 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 formatting/format.go create mode 100644 formatting/vcard.go rename {strings => formatting}/wifi.go (95%) create mode 100644 web/format.route.go rename web/{routeGenerate.go => generate.route.go} (87%) rename web/{help.go => help.route.go} (90%) diff --git a/.gitignore b/.gitignore index c38fa71..ee4e8a4 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/formatting/format.go b/formatting/format.go new file mode 100644 index 0000000..dea474b --- /dev/null +++ b/formatting/format.go @@ -0,0 +1 @@ +package formatting diff --git a/formatting/vcard.go b/formatting/vcard.go new file mode 100644 index 0000000..797d13d --- /dev/null +++ b/formatting/vcard.go @@ -0,0 +1,8 @@ +package formatting + +type Vcard struct { + FirstName string + Surname string + Mobile string + Home string +} diff --git a/strings/wifi.go b/formatting/wifi.go similarity index 95% rename from strings/wifi.go rename to formatting/wifi.go index 4cc0542..382bc0c 100644 --- a/strings/wifi.go +++ b/formatting/wifi.go @@ -1,4 +1,4 @@ -package strings +package formatting import "fmt" diff --git a/web/format.route.go b/web/format.route.go new file mode 100644 index 0000000..5fa884b --- /dev/null +++ b/web/format.route.go @@ -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) + }*/ +} diff --git a/web/routeGenerate.go b/web/generate.route.go similarity index 87% rename from web/routeGenerate.go rename to web/generate.route.go index 7314a07..4f81c39 100644 --- a/web/routeGenerate.go +++ b/web/generate.route.go @@ -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 { diff --git a/web/help.go b/web/help.route.go similarity index 90% rename from web/help.go rename to web/help.route.go index 46d7257..2ee3d12 100644 --- a/web/help.go +++ b/web/help.route.go @@ -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 { diff --git a/web/server.go b/web/server.go index eae5f2c..63767b1 100644 --- a/web/server.go +++ b/web/server.go @@ -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) diff --git a/web/types.go b/web/types.go index 4f13d70..68100e5 100644 --- a/web/types.go +++ b/web/types.go @@ -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"` +}