Updated with more query params

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
Marcus Noble 2023-03-03 09:06:20 +00:00
parent 489a398a7f
commit 9a2511e146
Signed by: AverageMarcus
GPG Key ID: B8F2DB8A7AEBAF78

45
main.go
View File

@ -2,20 +2,55 @@ package main
import ( import (
"fmt" "fmt"
qrcode "github.com/skip2/go-qrcode"
"net/http" "net/http"
"strconv"
qrcode "github.com/skip2/go-qrcode"
) )
func main() { func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
website := req.URL.Query().Get("website") data := ""
if website != "" { if req.URL.Query().Get("website") != "" {
png, _ := qrcode.Encode(website, qrcode.Medium, 80) data = req.URL.Query().Get("website")
} else if req.URL.Query().Get("url") != "" {
data = req.URL.Query().Get("url")
} else if req.URL.Query().Get("data") != "" {
data = req.URL.Query().Get("data")
} else if req.URL.Query().Get("text") != "" {
data = req.URL.Query().Get("text")
}
size := 150
if req.URL.Query().Get("size") != "" {
i, err := strconv.Atoi(req.URL.Query().Get("size"))
if err == nil {
size = i
}
}
disableBorder := false
if req.URL.Query().Get("disableBorder") != "" {
disableBorder = true
}
if data != "" {
code, _ := qrcode.New(data, qrcode.Medium)
code.DisableBorder = disableBorder
png, _ := code.PNG(size)
w.Header().Set("Content-Type", "image/png")
w.Write(png) w.Write(png)
return return
} }
w.WriteHeader(400) w.Write([]byte(`<html><head><title>QR Code</title></head><body>
<form action="/" method="GET" style="margin: 1em auto; text-align: center;">
<label>Website: <input type="text" name="website" /></label><br/>
<label>Size: <input type="text" name="size" value="150" /></label><br/>
<label>Disable border: <input type="checkbox" name="disableBorder" /></label><br/>
<input type="submit"/>
</form>
</body></html>`))
}) })
fmt.Println(http.ListenAndServe(":8080", nil)) fmt.Println(http.ListenAndServe(":8080", nil))
} }