diff --git a/go.mod b/go.mod index 44bcf44..4d7a8f1 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // 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/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect diff --git a/go.sum b/go.sum index 89cf1a4..f293fe3 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= diff --git a/http/main.go b/http/main.go index 5857b74..417499d 100644 --- a/http/main.go +++ b/http/main.go @@ -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, }) } diff --git a/http/qr.go b/http/qr.go new file mode 100644 index 0000000..f4e3b00 --- /dev/null +++ b/http/qr.go @@ -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 +} diff --git a/tmpl/thanks.html b/tmpl/thanks.html index c720df0..82af617 100644 --- a/tmpl/thanks.html +++ b/tmpl/thanks.html @@ -16,9 +16,12 @@ {{ .Id }}


-
- -
+
+ QR-Code +
+ + Go back +