💄 QR-Code for Vote-ID

This commit is contained in:
Brian Wiborg 2024-05-28 20:38:20 +02:00
parent 1c9937197f
commit 3d14d361c4
No known key found for this signature in database
GPG Key ID: BE53FA9286B719D6
5 changed files with 57 additions and 6 deletions

1
go.mod
View File

@ -17,6 +17,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/stretchr/testify v1.9.0 // indirect github.com/stretchr/testify v1.9.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect

2
go.sum
View File

@ -21,6 +21,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=

View File

@ -3,6 +3,7 @@ package http
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -18,6 +19,14 @@ import (
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
) )
var BASE_URL = os.Getenv("BASE_URL")
func init() {
if BASE_URL == "" {
BASE_URL = "http://localhost"
}
}
// Serve takes a bind-address and starts the HTTP server. // Serve takes a bind-address and starts the HTTP server.
func Serve(bindAddr string) error { func Serve(bindAddr string) error {
e := echo.New() e := echo.New()
@ -31,6 +40,7 @@ func Serve(bindAddr string) error {
e.GET("/v", handleVotingForm) e.GET("/v", handleVotingForm)
e.POST("/v", handleNewVoting) e.POST("/v", handleNewVoting)
e.GET("/v/:id", handleShowVoting) e.GET("/v/:id", handleShowVoting)
e.GET("v/:vid/:id", handleShowVote)
e.POST("/v/:id", handleVote) e.POST("/v/:id", handleVote)
return e.Start(bindAddr) return e.Start(bindAddr)
@ -125,10 +135,30 @@ func handleVote(ctx echo.Context) error {
return err return err
} }
store.PlaceVote(id, vid, elector, c) store.PlaceVote(id, vid, elector, c)
return ctx.Redirect(http.StatusFound, fmt.Sprintf("/v/%s/%s", vid, id))
}
func handleShowVote(ctx echo.Context) error {
var (
id = ctx.Param("id")
vid = ctx.Param("vid")
)
url := fmt.Sprintf(
"%s/v/%s/%s",
BASE_URL,
vid,
id,
)
qrCode, err := generateQrCode(url)
if err != nil {
return err
}
return ctx.Render(http.StatusFound, "thanks", map[string]interface{}{ return ctx.Render(http.StatusFound, "thanks", map[string]interface{}{
"Title": "cvote | thx", "Title": "cvote | nom nom nom",
"Id": id, "Id": id,
"Vid": vid, "Vid": vid,
"QRCode": qrCode,
}) })
} }

15
http/qr.go Normal file
View File

@ -0,0 +1,15 @@
package http
import (
b64 "encoding/base64"
"github.com/skip2/go-qrcode"
)
func generateQrCode(url string) (string, error) {
qrCode, err := qrcode.Encode(url, qrcode.Medium, 256)
if err != nil {
return "", err
}
return b64.StdEncoding.EncodeToString(qrCode), nil
}

View File

@ -16,9 +16,12 @@
{{ .Id }} {{ .Id }}
</p> </p>
<hr class="m-4"> <hr class="m-4">
<form action="/v/{{ .Vid }}" method="GET"> <div class="w-full flex justify-center">
<button type="submit" class="w-full hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center bg-purple-600 hover:bg-purple-700 focus:ring-blue-800 text-white hover:text-yellow-200 text-xl">Go back</button> <img src="data:image/png;base64,{{ .QRCode }}" alt="QR-Code" class="center pb-4">
</form> </div>
<a href="/v/{{ .Vid }}" class="w-full block hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center bg-purple-600 hover:bg-purple-700 focus:ring-blue-800 text-white hover:text-yellow-200 text-xl">
Go back
</a>
</div> </div>
</div> </div>
</div> </div>