Implement web-server

This commit is contained in:
Fred Boniface 2023-09-01 21:51:30 +01:00
parent 0e1425c1b9
commit f1c8eb82b8
6 changed files with 118 additions and 0 deletions

View File

@ -27,5 +27,10 @@ func Generate(parameters Parameters) (barcode.Barcode, error) {
fmt.Println("Unsupported barcode type: ", parameters.Format) 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 return barcode_content, err
} }

View File

@ -10,6 +10,7 @@ import (
"git.fjla.uk/fred.boniface/barcodes/generation" "git.fjla.uk/fred.boniface/barcodes/generation"
"git.fjla.uk/fred.boniface/barcodes/validation" "git.fjla.uk/fred.boniface/barcodes/validation"
"git.fjla.uk/fred.boniface/barcodes/web"
) )
func main() { func main() {
@ -46,4 +47,6 @@ func main() {
defer file.Close() defer file.Close()
png.Encode(file, generated_barcode) png.Encode(file, generated_barcode)
web.StartServer()
} }

View File

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

View File

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

78
web/server.go Normal file
View File

@ -0,0 +1,78 @@
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 StartServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Barcodes")
})
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)
}
}
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, req.Width, 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)
}
}

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"`
}