Add initial 'help' page

This commit is contained in:
Fred Boniface 2023-09-01 22:47:46 +01:00
parent 227bdd5dc8
commit 8e7350a355
5 changed files with 83 additions and 2 deletions

41
templates/help.html Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>API Documentation</title>
</head>
<body>
<h1>API Requests</h1>
<p>API Requests can be made by making a POST request to /generate</p>
<p>The request body should be JSON similar to the below</p>
<pre>
<code>
{
"barcode_type": "code128",
"width": 600,
"height": 100,
"ecc_level": 4,
"content": "45684562"
}
</code>
</pre>
<p><code>barcode_type</code> should be one of:</p>
<ul>
{{range .BarcodeOptions}}
<li>{{.}}</li>
{{end}}
</ul>
<p><code>width</code> and <code>height</code> are in pixels</p>
<p><code>ecc_level</code> should be 1, 2, 3 or 4 where 1 is least resilient and 4 is most resilient</p>
<p><code>content</code> should be the value you wish the barcode to display. This may not accept all characters - that is dependent on the barcode type.</p>
<p>The response will be a PNG image</p>
</body>
</html>

View File

@ -11,7 +11,7 @@ type rule struct {
divisible uint8
}
var formatRules = map[string]rule{
var FormatRules = map[string]rule{
"2of5": {
minLength: 2,

View File

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

38
web/help.go Normal file
View File

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

View File

@ -10,6 +10,8 @@ func StartServer() {
fmt.Fprintf(w, "Barcodes")
})
http.HandleFunc("/help", buildPage)
http.HandleFunc("/generate", generateBarcode)
port := ":8500"