Compare commits

...

2 Commits

10 changed files with 100 additions and 13 deletions

3
.gitignore vendored
View File

@ -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

1
formatting/format.go Normal file
View File

@ -0,0 +1 @@
package formatting

8
formatting/vcard.go Normal file
View File

@ -0,0 +1,8 @@
package formatting
type Vcard struct {
FirstName string
Surname string
Mobile string
Home string
}

View File

@ -1,4 +1,4 @@
package strings
package formatting
import "fmt"

View File

@ -1,29 +1,33 @@
package validation
import "fmt"
import (
"fmt"
func Validate(input string, format string) bool {
rule, exists := FormatRules[format]
"git.fjla.uk/fred.boniface/barcodes/generation"
)
func Validate(params generation.Parameters) bool {
rule, exists := FormatRules[string(params.Format)]
if !exists {
fmt.Printf("Error: No rule found for format '%s'\n", format)
fmt.Printf("Error: No rule found for format '%s'\n", params.Format)
return false
}
if uint64(len(input)) < rule.minLength || uint64(len(input)) > rule.maxLength {
if uint64(len(params.Content)) < rule.minLength || uint64(len(params.Content)) > rule.maxLength {
fmt.Printf("Validation Error: Length must be between %d and %d\n", rule.minLength, rule.maxLength)
return false
}
if !rule.charset.MatchString(input) {
if !rule.charset.MatchString(params.Content) {
fmt.Println("Validation Error: Contains illegal characters")
return false
}
if uint64(len(input))%uint64(rule.divisible) != 0 {
if uint64(len(params.Content))%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", format)
fmt.Printf("Validation passed for barcode type %s\n", params.Format)
return true
}

56
web/format.route.go Normal file
View 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)
}*/
}

View File

@ -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 {
@ -26,7 +32,7 @@ func generateBarcode(w http.ResponseWriter, r *http.Request) {
Content: req.Content,
}
validate := validation.Validate(req.Content, req.BarcodeType)
validate := validation.Validate(parameters)
if !validate {
fmt.Println("Validation Failed")
http.Error(w, "Validation Failed", http.StatusBadRequest)

View File

@ -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 {

View File

@ -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)

View File

@ -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"`
}