diff --git a/templates/help.html b/templates/help.html new file mode 100644 index 0000000..891f5e1 --- /dev/null +++ b/templates/help.html @@ -0,0 +1,41 @@ + + +
+ +API Requests can be made by making a POST request to /generate
+ +The request body should be JSON similar to the below
+ +
+
+ {
+ "barcode_type": "code128",
+ "width": 600,
+ "height": 100,
+ "ecc_level": 4,
+ "content": "45684562"
+ }
+
+
+
+ barcode_type
should be one of:
width
and height
are in pixels
ecc_level
should be 1, 2, 3 or 4 where 1 is least resilient and 4 is most resilient
content
should be the value you wish the barcode to display. This may not accept all characters - that is dependent on the barcode type.
The response will be a PNG image
+ + diff --git a/validation/rules.go b/validation/rules.go index de0f88b..dd71c67 100644 --- a/validation/rules.go +++ b/validation/rules.go @@ -11,7 +11,7 @@ type rule struct { divisible uint8 } -var formatRules = map[string]rule{ +var FormatRules = map[string]rule{ "2of5": { minLength: 2, diff --git a/validation/validate.go b/validation/validate.go index fb7b48f..4c1aea2 100644 --- a/validation/validate.go +++ b/validation/validate.go @@ -3,7 +3,7 @@ package validation import "fmt" func Validate(input string, format string) bool { - rule, exists := formatRules[format] + rule, exists := FormatRules[format] if !exists { fmt.Printf("Error: No rule found for format '%s'\n", format) return false diff --git a/web/help.go b/web/help.go new file mode 100644 index 0000000..d374a59 --- /dev/null +++ b/web/help.go @@ -0,0 +1,38 @@ +package web + +import ( + "fmt" + "html/template" + "net/http" + + "git.fjla.uk/fred.boniface/barcodes/validation" +) + +func buildPage(w http.ResponseWriter, r *http.Request) { + i := 0 + barcodeOptions := make([]string, len(validation.FormatRules)) + for k := range validation.FormatRules { + barcodeOptions[i] = k + i++ + } + + type dataType struct { + BarcodeOptions []string + } + data := dataType{ + BarcodeOptions: barcodeOptions, + } + + tmpl, err := template.New("help").ParseFiles("templates/help.html") // Match template name and file name + if err != nil { + fmt.Println("Error templating page: ", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + err = tmpl.Execute(w, data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/web/server.go b/web/server.go index 2498929..eae5f2c 100644 --- a/web/server.go +++ b/web/server.go @@ -10,6 +10,8 @@ func StartServer() { fmt.Fprintf(w, "Barcodes") }) + http.HandleFunc("/help", buildPage) + http.HandleFunc("/generate", generateBarcode) port := ":8500"