barcodes/web/format.route.go

57 lines
1.5 KiB
Go

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)
}*/
}