Compare commits
No commits in common. "4dad1406eb84145186d34b000828181fae6b86fa" and "cc08f8358e8016a11affa2efa30c747077b102b9" have entirely different histories.
4dad1406eb
...
cc08f8358e
|
@ -1,5 +1,4 @@
|
|||
# Compiled binaries
|
||||
barcodes
|
||||
*.png
|
||||
|
||||
# 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 +0,0 @@
|
|||
package formatting
|
|
@ -1,8 +0,0 @@
|
|||
package formatting
|
||||
|
||||
type Vcard struct {
|
||||
FirstName string
|
||||
Surname string
|
||||
Mobile string
|
||||
Home string
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package formatting
|
||||
package strings
|
||||
|
||||
import "fmt"
|
||||
|
|
@ -1,33 +1,29 @@
|
|||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
import "fmt"
|
||||
|
||||
"git.fjla.uk/fred.boniface/barcodes/generation"
|
||||
)
|
||||
|
||||
func Validate(params generation.Parameters) bool {
|
||||
rule, exists := FormatRules[string(params.Format)]
|
||||
func Validate(input string, format string) bool {
|
||||
rule, exists := FormatRules[format]
|
||||
if !exists {
|
||||
fmt.Printf("Error: No rule found for format '%s'\n", params.Format)
|
||||
fmt.Printf("Error: No rule found for format '%s'\n", format)
|
||||
return false
|
||||
}
|
||||
|
||||
if uint64(len(params.Content)) < rule.minLength || uint64(len(params.Content)) > rule.maxLength {
|
||||
if uint64(len(input)) < rule.minLength || uint64(len(input)) > rule.maxLength {
|
||||
fmt.Printf("Validation Error: Length must be between %d and %d\n", rule.minLength, rule.maxLength)
|
||||
return false
|
||||
}
|
||||
|
||||
if !rule.charset.MatchString(params.Content) {
|
||||
if !rule.charset.MatchString(input) {
|
||||
fmt.Println("Validation Error: Contains illegal characters")
|
||||
return false
|
||||
}
|
||||
|
||||
if uint64(len(params.Content))%uint64(rule.divisible) != 0 {
|
||||
if uint64(len(input))%uint64(rule.divisible) != 0 {
|
||||
fmt.Printf("Validation Error: Length must be a multiple of %d", rule.divisible)
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Printf("Validation passed for barcode type %s\n", params.Format)
|
||||
fmt.Printf("Validation passed for barcode type %s\n", format)
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
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)
|
||||
}*/
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
"git.fjla.uk/fred.boniface/barcodes/validation"
|
||||
)
|
||||
|
||||
func helpTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
func buildPage(w http.ResponseWriter, r *http.Request) {
|
||||
i := 0
|
||||
barcodeOptions := make([]string, len(validation.FormatRules))
|
||||
for k := range validation.FormatRules {
|
|
@ -12,13 +12,7 @@ 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 {
|
||||
|
@ -32,7 +26,7 @@ func generateBarcode(w http.ResponseWriter, r *http.Request) {
|
|||
Content: req.Content,
|
||||
}
|
||||
|
||||
validate := validation.Validate(parameters)
|
||||
validate := validation.Validate(req.Content, req.BarcodeType)
|
||||
if !validate {
|
||||
fmt.Println("Validation Failed")
|
||||
http.Error(w, "Validation Failed", http.StatusBadRequest)
|
|
@ -10,12 +10,10 @@ func StartServer() {
|
|||
fmt.Fprintf(w, "Barcodes")
|
||||
})
|
||||
|
||||
http.HandleFunc("/help", helpTemplate)
|
||||
http.HandleFunc("/help", buildPage)
|
||||
|
||||
http.HandleFunc("/generate", generateBarcode)
|
||||
|
||||
http.HandleFunc("/generate/formatted", generateFormattedBarcode)
|
||||
|
||||
port := ":8500"
|
||||
fmt.Printf("Server listening on port %s\n", port)
|
||||
|
||||
|
|
|
@ -7,12 +7,3 @@ 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"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue