Compare commits

..

No commits in common. "4dad1406eb84145186d34b000828181fae6b86fa" and "cc08f8358e8016a11affa2efa30c747077b102b9" have entirely different histories.

10 changed files with 13 additions and 100 deletions

3
.gitignore vendored
View File

@ -1,5 +1,4 @@
# Compiled binaries *.png
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

View File

@ -1 +0,0 @@
package formatting

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ import (
"git.fjla.uk/fred.boniface/barcodes/validation" "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 i := 0
barcodeOptions := make([]string, len(validation.FormatRules)) barcodeOptions := make([]string, len(validation.FormatRules))
for k := range validation.FormatRules { for k := range validation.FormatRules {

View File

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

View File

@ -10,12 +10,10 @@ func StartServer() {
fmt.Fprintf(w, "Barcodes") fmt.Fprintf(w, "Barcodes")
}) })
http.HandleFunc("/help", helpTemplate) http.HandleFunc("/help", buildPage)
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)

View File

@ -7,12 +7,3 @@ 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"`
}