diff --git a/templates/help.html b/templates/help.html index 0ab8b15..54087b3 100644 --- a/templates/help.html +++ b/templates/help.html @@ -4,7 +4,12 @@ {{ end }} {{ define "body" }} -

API Requests can be made by making a POST request to /generate

+

For simple textual QR, Aztec or DataMatrix codes, a GET request can be made as follows:

+ /generate?type=[qr|aztec|datamatrix]&text=[YOUR_TEXT] +

Dimensions will default to 300x300, and ECC level to 2.

+

This is also the format for URL barcodes

+ +

For other barcode types and custom options, API Requests can be made by making a POST request to /generate

The request body should be JSON similar to the below

@@ -35,6 +40,7 @@

The response will be a PNG image

+

Formatted Barcodes

Formatted barcodes are under development, this API endpoint /generate/format will produce a QR, Aztec or Datamatrix barcode with one of the following formats:

@@ -47,4 +53,5 @@
  • Phone Number
  • Note that URLs have no special formatting and can be produced as plain text barcodes using the /generate endpoint

    +

    This endpoint is not yet available

    {{ end }} \ No newline at end of file diff --git a/web/generate.route.go b/web/generate.route.go index 4f81c39..8df656b 100644 --- a/web/generate.route.go +++ b/web/generate.route.go @@ -2,6 +2,7 @@ package web import ( "encoding/json" + "errors" "fmt" "image" "image/png" @@ -12,18 +13,29 @@ import ( "github.com/boombuler/barcode" ) -// Generates a barcode from the data and options provided in the request body +// Generates a barcode from the data and options provided in the 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) + + if r.Method != http.MethodPost && r.Method != http.MethodGet { + http.Error(w, "Use a POST or GET request for barcode generation. See the help page for more information", http.StatusMethodNotAllowed) return } - var req BarcodeRequest - err := json.NewDecoder(r.Body).Decode(&req) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return + var req *BarcodeRequest + var err error + + if r.Method == http.MethodGet { + req, err = generateFromGet(r) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + } else { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } } parameters := generation.Parameters{ @@ -66,3 +78,37 @@ func generateBarcode(w http.ResponseWriter, r *http.Request) { http.Error(w, "Error streaming barcode", http.StatusInternalServerError) } } + +func generateFromGet(r *http.Request) (*BarcodeRequest, error) { + //var parameters generation.Parameters + if r.Method == http.MethodGet { + validTypes := map[string]bool{ + "qr": true, + "aztec": true, + "datamatrix": true, + } + + typeParam := r.URL.Query().Get("type") + content := r.URL.Query().Get("text") + + if typeParam == "" || content == "" { + return nil, errors.New("both 'type' and 'text' parameters are required") + } + + if !validTypes[typeParam] { + return nil, errors.New("'type' must be 'qr', 'aztec', or 'datamatrix'") + } + + barcodeReq := &BarcodeRequest{ + BarcodeType: typeParam, + Width: 300, + Height: 300, + ECCLevel: 2, + Content: content, + } + + return barcodeReq, nil + } else { + return nil, errors.New("incorrect request type passed to function") + } +}