Compare commits
No commits in common. "8e7350a3557c4767959b48f13e73527c83b68fdc" and "53cae5ebe67adb8ee7f4011efbc3432527bde800" have entirely different histories.
8e7350a355
...
53cae5ebe6
|
@ -1,23 +0,0 @@
|
||||||
*.png
|
|
||||||
|
|
||||||
# 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
|
|
||||||
#
|
|
||||||
# Binaries for programs and plugins
|
|
||||||
*.exe
|
|
||||||
*.exe~
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
|
||||||
*.test
|
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
|
||||||
*.out
|
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
|
||||||
# vendor/
|
|
||||||
|
|
||||||
# Go workspace file
|
|
||||||
go.work
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/twooffive"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generate2of5(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
barcode, err := twooffive.Encode(parameters.Content, false)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return barcode, err
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/twooffive"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generate2of5Interleaved(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
barcode, err := twooffive.Encode(parameters.Content, true)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return barcode, err
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/aztec"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateAztec(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
var level uint8
|
|
||||||
switch parameters.ECCLevel {
|
|
||||||
case 1:
|
|
||||||
level = 7
|
|
||||||
case 2:
|
|
||||||
level = 15
|
|
||||||
case 3:
|
|
||||||
level = 25
|
|
||||||
case 4:
|
|
||||||
level = 30
|
|
||||||
}
|
|
||||||
aztecCode, err := aztec.Encode([]byte(parameters.Content), int(level), 0)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return aztecCode, err
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/codabar"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateCodabar(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
codabarItem, err := codabar.Encode(parameters.Content)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return codabarItem, err
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/code128"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateCode128(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
barcode, err := code128.EncodeWithoutChecksum(parameters.Content)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return barcode, err
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/code93"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateCode93(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
barcode, err := code93.Encode(parameters.Content, true, true)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return barcode, err
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/datamatrix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateDatamatrix(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
barcode, err := datamatrix.Encode(parameters.Content)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
return barcode, err
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Generate(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
var barcode_content barcode.Barcode
|
|
||||||
var err error
|
|
||||||
|
|
||||||
switch parameters.Format {
|
|
||||||
case Aztec:
|
|
||||||
barcode_content, err = generateAztec(parameters)
|
|
||||||
case Codabar:
|
|
||||||
barcode_content, err = generateCodabar(parameters)
|
|
||||||
case Code93:
|
|
||||||
barcode_content, err = generateCode93(parameters)
|
|
||||||
case Code128:
|
|
||||||
barcode_content, err = generateCode128(parameters)
|
|
||||||
case QR:
|
|
||||||
barcode_content, err = generateQr(parameters)
|
|
||||||
case Datamatrix:
|
|
||||||
barcode_content, err = generateDatamatrix(parameters)
|
|
||||||
case PDF417:
|
|
||||||
barcode_content, err = generatePDF417(parameters)
|
|
||||||
case TwoOfFiveInterleaved:
|
|
||||||
barcode_content, err = generate2of5Interleaved(parameters)
|
|
||||||
case TwoOfFive:
|
|
||||||
barcode_content, err = generate2of5(parameters)
|
|
||||||
default:
|
|
||||||
fmt.Println("Unsupported barcode type: ", parameters.Format)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Generation Error: ", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fmt.Println("Barcode Generated")
|
|
||||||
return barcode_content, err
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/pdf417"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generatePDF417(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
var level uint8
|
|
||||||
switch parameters.ECCLevel {
|
|
||||||
case 1:
|
|
||||||
level = byte(1)
|
|
||||||
case 2:
|
|
||||||
level = byte(3)
|
|
||||||
case 3:
|
|
||||||
level = byte(5)
|
|
||||||
case 4:
|
|
||||||
level = byte(8)
|
|
||||||
}
|
|
||||||
pdf417Code, err := pdf417.Encode(parameters.Content, level)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return pdf417Code, err
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
"github.com/boombuler/barcode/qr"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateQr(parameters Parameters) (barcode.Barcode, error) {
|
|
||||||
var level qr.ErrorCorrectionLevel
|
|
||||||
switch parameters.ECCLevel {
|
|
||||||
case 1:
|
|
||||||
level = qr.L
|
|
||||||
case 2:
|
|
||||||
level = qr.M
|
|
||||||
case 3:
|
|
||||||
level = qr.Q
|
|
||||||
case 4:
|
|
||||||
level = qr.H
|
|
||||||
}
|
|
||||||
qrCode, err := qr.Encode(parameters.Content, level, qr.Auto)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating Barcode", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return qrCode, err
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
package generation
|
|
||||||
|
|
||||||
type BarcodeType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Aztec BarcodeType = "aztec"
|
|
||||||
Codabar BarcodeType = "codabar"
|
|
||||||
Code93 BarcodeType = "code93"
|
|
||||||
Code128 BarcodeType = "code128"
|
|
||||||
QR BarcodeType = "qr"
|
|
||||||
Datamatrix BarcodeType = "datamatrix"
|
|
||||||
PDF417 BarcodeType = "pdf417"
|
|
||||||
TwoOfFive BarcodeType = "2of5"
|
|
||||||
TwoOfFiveInterleaved BarcodeType = "2of5interleaved"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ECCLevel int
|
|
||||||
|
|
||||||
const (
|
|
||||||
Low ECCLevel = 1
|
|
||||||
Med ECCLevel = 2
|
|
||||||
High ECCLevel = 3
|
|
||||||
Max ECCLevel = 4
|
|
||||||
)
|
|
||||||
|
|
||||||
type Parameters struct {
|
|
||||||
Format BarcodeType
|
|
||||||
ECCLevel ECCLevel
|
|
||||||
Content string
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
2
go.mod
2
go.mod
|
@ -1,5 +1,3 @@
|
||||||
module git.fjla.uk/fred.boniface/barcodes
|
module git.fjla.uk/fred.boniface/barcodes
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require github.com/boombuler/barcode v1.0.1
|
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,2 +0,0 @@
|
||||||
github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs=
|
|
||||||
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
|
12
main.go
12
main.go
|
@ -1,12 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.fjla.uk/fred.boniface/barcodes/web"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println("Starting `barcodes` server")
|
|
||||||
web.StartServer()
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
package strings
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type Encryption string
|
|
||||||
|
|
||||||
const (
|
|
||||||
WEP Encryption = "wep"
|
|
||||||
WPA Encryption = "wpa"
|
|
||||||
None Encryption = ""
|
|
||||||
)
|
|
||||||
|
|
||||||
type WiFi struct {
|
|
||||||
Ssid string
|
|
||||||
Password string
|
|
||||||
Hidden bool
|
|
||||||
Encryption Encryption
|
|
||||||
}
|
|
||||||
|
|
||||||
func FormatWiFi(input WiFi) string {
|
|
||||||
content := fmt.Sprintf("WIFI:T:%s;S:%s;P:%s;H:%t;", input.Encryption, input.Ssid, input.Password, input.Hidden)
|
|
||||||
return content
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
<!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>
|
|
|
@ -5,9 +5,7 @@ import "regexp"
|
||||||
var (
|
var (
|
||||||
ASCII = regexp.MustCompile(`^[\x00-\x7F]+$`)
|
ASCII = regexp.MustCompile(`^[\x00-\x7F]+$`)
|
||||||
ANY = regexp.MustCompile(`.`)
|
ANY = regexp.MustCompile(`.`)
|
||||||
CODABAR = regexp.MustCompile(`^[0-9-$:/.+]+$`)
|
|
||||||
CODE39 = regexp.MustCompile(`^[0-9A-Z\-\.\ $%*+/]+$`)
|
CODE39 = regexp.MustCompile(`^[0-9A-Z\-\.\ $%*+/]+$`)
|
||||||
CODE93 = regexp.MustCompile(`^[A-Za-z0-9\-.\$/\+%\s]+$`)
|
|
||||||
NUMERAL = regexp.MustCompile(`^[0-9]+$`)
|
NUMERAL = regexp.MustCompile(`^[0-9]+$`)
|
||||||
ISO88591 = regexp.MustCompile(`^[\x00-\xFF]+$`)
|
ISO88591 = regexp.MustCompile(`^[\x00-\xFF]+$`)
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,89 +11,79 @@ type rule struct {
|
||||||
divisible uint8
|
divisible uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
var FormatRules = map[string]rule{
|
var aztec = rule{
|
||||||
|
|
||||||
"2of5": {
|
|
||||||
minLength: 2,
|
|
||||||
maxLength: 80,
|
|
||||||
charset: *NUMERAL,
|
|
||||||
divisible: 1,
|
|
||||||
},
|
|
||||||
|
|
||||||
"2of5interleaved": {
|
|
||||||
minLength: 2,
|
|
||||||
maxLength: 80,
|
|
||||||
charset: *NUMERAL,
|
|
||||||
divisible: 2,
|
|
||||||
},
|
|
||||||
|
|
||||||
"aztec": {
|
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 3067,
|
maxLength: 3067,
|
||||||
charset: *ANY,
|
charset: *ANY,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"codabar": {
|
var code39 = rule{
|
||||||
minLength: 1,
|
|
||||||
maxLength: 100,
|
|
||||||
charset: *CODABAR,
|
|
||||||
divisible: 1,
|
|
||||||
},
|
|
||||||
|
|
||||||
"code39": {
|
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 80,
|
maxLength: 80,
|
||||||
charset: *CODE39,
|
charset: *CODE39,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"code93": {
|
var code128 = rule{
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 80,
|
maxLength: 80,
|
||||||
charset: *ASCII,
|
charset: *ASCII,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"code128": {
|
var datamatrix = rule{
|
||||||
minLength: 1,
|
|
||||||
maxLength: 80,
|
|
||||||
charset: *ASCII,
|
|
||||||
divisible: 1,
|
|
||||||
},
|
|
||||||
|
|
||||||
"datamatrix": {
|
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 2335,
|
maxLength: 2335,
|
||||||
charset: *ANY,
|
charset: *ANY,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"ean8": {
|
var ean8 = rule{
|
||||||
minLength: 7,
|
minLength: 7,
|
||||||
maxLength: 7,
|
maxLength: 7,
|
||||||
charset: *NUMERAL,
|
charset: *NUMERAL,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"ean13": {
|
var ean13 = rule{
|
||||||
minLength: 12,
|
minLength: 12,
|
||||||
maxLength: 12,
|
maxLength: 12,
|
||||||
charset: *NUMERAL,
|
charset: *NUMERAL,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"pdf417": {
|
var ean14 = rule{
|
||||||
|
minLength: 2,
|
||||||
|
maxLength: 80,
|
||||||
|
charset: *NUMERAL,
|
||||||
|
divisible: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
var pdf417 = rule{
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 1100,
|
maxLength: 1100,
|
||||||
charset: *ANY,
|
charset: *ANY,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
"qr": {
|
var pzn7 = rule{
|
||||||
|
minLength: 7,
|
||||||
|
maxLength: 7,
|
||||||
|
charset: *NUMERAL,
|
||||||
|
divisible: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
var qr = rule{
|
||||||
minLength: 1,
|
minLength: 1,
|
||||||
maxLength: 4296,
|
maxLength: 4296,
|
||||||
charset: *ISO88591,
|
charset: *ISO88591,
|
||||||
divisible: 1,
|
divisible: 1,
|
||||||
},
|
}
|
||||||
|
|
||||||
|
var upca = rule{
|
||||||
|
minLength: 11,
|
||||||
|
maxLength: 11,
|
||||||
|
charset: *NUMERAL,
|
||||||
|
divisible: 1,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package validation
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func Validate(input string, format string) bool {
|
func Validate(input string, format string) bool {
|
||||||
rule, exists := FormatRules[format]
|
rule, exists := barcodeRules[format]
|
||||||
if !exists {
|
if !exists {
|
||||||
fmt.Printf("Error: No rule found for format '%s'\n", format)
|
fmt.Printf("Error: No rule found for format '%s'\n", format)
|
||||||
return false
|
return false
|
||||||
|
@ -24,6 +24,5 @@ func Validate(input string, format string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Validation passed for barcode type %s\n", format)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
38
web/help.go
38
web/help.go
|
@ -1,38 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,62 +0,0 @@
|
||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"image"
|
|
||||||
"image/png"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.fjla.uk/fred.boniface/barcodes/generation"
|
|
||||||
"git.fjla.uk/fred.boniface/barcodes/validation"
|
|
||||||
"github.com/boombuler/barcode"
|
|
||||||
)
|
|
||||||
|
|
||||||
func generateBarcode(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var req BarcodeRequest
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
parameters := generation.Parameters{
|
|
||||||
Format: generation.BarcodeType(req.BarcodeType),
|
|
||||||
ECCLevel: generation.ECCLevel(req.ECCLevel),
|
|
||||||
Content: req.Content,
|
|
||||||
}
|
|
||||||
|
|
||||||
validate := validation.Validate(req.Content, req.BarcodeType)
|
|
||||||
if !validate {
|
|
||||||
fmt.Println("Validation Failed")
|
|
||||||
http.Error(w, "Validation Failed", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func StartServer() {
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprintf(w, "Barcodes")
|
|
||||||
})
|
|
||||||
|
|
||||||
http.HandleFunc("/help", buildPage)
|
|
||||||
|
|
||||||
http.HandleFunc("/generate", generateBarcode)
|
|
||||||
|
|
||||||
port := ":8500"
|
|
||||||
fmt.Printf("Server listening on port %s\n", port)
|
|
||||||
|
|
||||||
err := http.ListenAndServe(port, nil)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package web
|
|
||||||
|
|
||||||
type BarcodeRequest struct {
|
|
||||||
BarcodeType string `json:"barcode_type"`
|
|
||||||
Width int `json:"width"`
|
|
||||||
Height int `json:"height"`
|
|
||||||
ECCLevel int `json:"ecc_level"`
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
Loading…
Reference in New Issue