💄 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

View file

@ -3,6 +3,7 @@ package http
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
@ -18,6 +19,14 @@ import (
"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.
func Serve(bindAddr string) error {
e := echo.New()
@ -31,6 +40,7 @@ func Serve(bindAddr string) error {
e.GET("/v", handleVotingForm)
e.POST("/v", handleNewVoting)
e.GET("/v/:id", handleShowVoting)
e.GET("v/:vid/:id", handleShowVote)
e.POST("/v/:id", handleVote)
return e.Start(bindAddr)
@ -125,10 +135,30 @@ func handleVote(ctx echo.Context) error {
return err
}
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{}{
"Title": "cvote | thx",
"Id": id,
"Vid": vid,
"Title": "cvote | nom nom nom",
"Id": id,
"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
}