Compare commits

...

5 Commits

Author SHA1 Message Date
Fred Boniface 8e7350a355 Add initial 'help' page 2023-09-01 22:47:46 +01:00
Fred Boniface 227bdd5dc8 Add starting print line 2023-09-01 22:15:40 +01:00
Fred Boniface d236acb2d7 Add 2of5 and pdf417 2023-09-01 22:14:11 +01:00
Fred Boniface f1c8eb82b8 Implement web-server 2023-09-01 21:51:30 +01:00
Fred Boniface 0e1425c1b9 Implement creation 2023-09-01 20:50:47 +01:00
24 changed files with 569 additions and 66 deletions

23
.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
*.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

16
generation/2of5.go Normal file
View File

@ -0,0 +1,16 @@
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
}

View File

@ -0,0 +1,16 @@
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
}

28
generation/aztec.go Normal file
View File

@ -0,0 +1,28 @@
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
}

16
generation/codabar.go Normal file
View File

@ -0,0 +1,16 @@
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
}

16
generation/code128.go Normal file
View File

@ -0,0 +1,16 @@
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
}

16
generation/code93.go Normal file
View File

@ -0,0 +1,16 @@
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
}

16
generation/datamatrix.go Normal file
View File

@ -0,0 +1,16 @@
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
}

42
generation/generate.go Normal file
View File

@ -0,0 +1,42 @@
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
}

28
generation/pdf417.go Normal file
View File

@ -0,0 +1,28 @@
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
}

28
generation/qr.go Normal file
View File

@ -0,0 +1,28 @@
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
}

32
generation/types.go Normal file
View File

@ -0,0 +1,32 @@
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
View File

@ -1,3 +1,5 @@
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 Normal file
View File

@ -0,0 +1,2 @@
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 Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"fmt"
"git.fjla.uk/fred.boniface/barcodes/web"
)
func main() {
fmt.Println("Starting `barcodes` server")
web.StartServer()
}

23
strings/wifi.go Normal file
View File

@ -0,0 +1,23 @@
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
}

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

@ -5,7 +5,9 @@ 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]+$`)
) )

View File

@ -11,79 +11,89 @@ type rule struct {
divisible uint8 divisible uint8
} }
var aztec = rule{ var FormatRules = map[string]rule{
minLength: 1,
maxLength: 3067,
charset: *ANY,
divisible: 1,
}
var code39 = rule{ "2of5": {
minLength: 1, minLength: 2,
maxLength: 80, maxLength: 80,
charset: *CODE39,
divisible: 1,
}
var code128 = rule{
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
}
var datamatrix = rule{
minLength: 1,
maxLength: 2335,
charset: *ANY,
divisible: 1,
}
var ean8 = rule{
minLength: 7,
maxLength: 7,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 1, divisible: 1,
} },
var ean13 = rule{ "2of5interleaved": {
minLength: 12,
maxLength: 12,
charset: *NUMERAL,
divisible: 1,
}
var ean14 = rule{
minLength: 2, minLength: 2,
maxLength: 80, maxLength: 80,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 2, divisible: 2,
} },
var pdf417 = rule{ "aztec": {
minLength: 1, minLength: 1,
maxLength: 1100, maxLength: 3067,
charset: *ANY, charset: *ANY,
divisible: 1, divisible: 1,
} },
var pzn7 = rule{ "codabar": {
minLength: 1,
maxLength: 100,
charset: *CODABAR,
divisible: 1,
},
"code39": {
minLength: 1,
maxLength: 80,
charset: *CODE39,
divisible: 1,
},
"code93": {
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
},
"code128": {
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
},
"datamatrix": {
minLength: 1,
maxLength: 2335,
charset: *ANY,
divisible: 1,
},
"ean8": {
minLength: 7, minLength: 7,
maxLength: 7, maxLength: 7,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 1, divisible: 1,
} },
var qr = rule{ "ean13": {
minLength: 12,
maxLength: 12,
charset: *NUMERAL,
divisible: 1,
},
"pdf417": {
minLength: 1,
maxLength: 1100,
charset: *ANY,
divisible: 1,
},
"qr": {
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,
} }

View File

@ -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 := barcodeRules[format] rule, exists := FormatRules[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,5 +24,6 @@ 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 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
}
}

62
web/routeGenerate.go Normal file
View File

@ -0,0 +1,62 @@
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)
}
}

24
web/server.go Normal file
View File

@ -0,0 +1,24 @@
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)
}
}

9
web/types.go Normal file
View File

@ -0,0 +1,9 @@
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"`
}