Begin implementation of formatted barcodes
This commit is contained in:
parent
d3bd2673e8
commit
4dad1406eb
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
*.png
|
# Compiled binaries
|
||||||
|
barcodes
|
||||||
|
|
||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
# 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
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
1
formatting/format.go
Normal file
1
formatting/format.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package formatting
|
8
formatting/vcard.go
Normal file
8
formatting/vcard.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package formatting
|
||||||
|
|
||||||
|
type Vcard struct {
|
||||||
|
FirstName string
|
||||||
|
Surname string
|
||||||
|
Mobile string
|
||||||
|
Home string
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package strings
|
package formatting
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
56
web/format.route.go
Normal file
56
web/format.route.go
Normal 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)
|
||||||
|
}*/
|
||||||
|
}
|
@ -12,7 +12,13 @@ import (
|
|||||||
"github.com/boombuler/barcode"
|
"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) {
|
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
|
var req BarcodeRequest
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
if err != nil {
|
if err != nil {
|
@ -7,7 +7,7 @@ import (
|
|||||||
"git.fjla.uk/fred.boniface/barcodes/validation"
|
"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
|
i := 0
|
||||||
barcodeOptions := make([]string, len(validation.FormatRules))
|
barcodeOptions := make([]string, len(validation.FormatRules))
|
||||||
for k := range validation.FormatRules {
|
for k := range validation.FormatRules {
|
@ -10,10 +10,12 @@ func StartServer() {
|
|||||||
fmt.Fprintf(w, "Barcodes")
|
fmt.Fprintf(w, "Barcodes")
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/help", buildPage)
|
http.HandleFunc("/help", helpTemplate)
|
||||||
|
|
||||||
http.HandleFunc("/generate", generateBarcode)
|
http.HandleFunc("/generate", generateBarcode)
|
||||||
|
|
||||||
|
http.HandleFunc("/generate/formatted", generateFormattedBarcode)
|
||||||
|
|
||||||
port := ":8500"
|
port := ":8500"
|
||||||
fmt.Printf("Server listening on port %s\n", port)
|
fmt.Printf("Server listening on port %s\n", port)
|
||||||
|
|
||||||
|
@ -7,3 +7,12 @@ type BarcodeRequest struct {
|
|||||||
ECCLevel int `json:"ecc_level"`
|
ECCLevel int `json:"ecc_level"`
|
||||||
Content string `json:"content"`
|
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"`
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user