2023-09-01 22:14:11 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-09-02 22:05:46 +01:00
|
|
|
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
|
2023-09-01 22:14:11 +01:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:05:46 +01:00
|
|
|
// Define a new type in `formatting` and create an instance of it here
|
2023-09-01 22:14:11 +01:00
|
|
|
|
2023-09-02 22:05:46 +01:00
|
|
|
// Call the format function providing the above type
|
|
|
|
|
|
|
|
// Create a generation.Parameters struct here using the string obtained from `formatting`
|
2023-09-01 22:14:11 +01:00
|
|
|
|
2023-09-02 22:05:46 +01:00
|
|
|
// Call the validate function providing the generation.Parameters var
|
|
|
|
|
|
|
|
/* Generate the barcode as per generate.route.go
|
2023-09-01 22:14:11 +01:00
|
|
|
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)
|
2023-09-02 22:05:46 +01:00
|
|
|
}*/
|
2023-09-01 22:14:11 +01:00
|
|
|
}
|