From b06dd9afb4ea7dd41e5a800d962494f17ae7ddc9 Mon Sep 17 00:00:00 2001 From: Danijel Fischer Date: Sat, 13 Jun 2020 13:16:23 +0200 Subject: [PATCH] Add endpoint for QR Code generation --- README.md | 12 +++++++++++- src/main.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e677fe0..e1c030d 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,19 @@ The group id can be obtained via the "List groups" REST call. ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/link/HomeAssistant'``` - This provides a tsdevice link which have to be converted to a QR-Code, e.g. `qrencode -o linkqr.png tsdevice:...` + This provides a tsdevice link which have to be converted to a QR-Code. For this the endpoint /v1/qrcode/ ca be used. +* Get QR Code + + ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/qrcode/'``` + + e.g: + + ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/qrcode/tsdevice:?uuid=ZiVIV..'``` + + This provides a QR-Code image. In case of an error a JSON object will be returned. + The following REST API endpoints are **deprecated and no longer maintained!** diff --git a/src/main.go b/src/main.go index 575ef1d..0c8c19a 100644 --- a/src/main.go +++ b/src/main.go @@ -14,7 +14,9 @@ import ( "github.com/gin-gonic/gin" "github.com/h2non/filetype" + uuid "github.com/satori/go.uuid" log "github.com/sirupsen/logrus" + qrcode "github.com/skip2/go-qrcode" ) const groupPrefix = "group." @@ -496,5 +498,23 @@ func main() { } }) + router.GET("/v1/qrcode/:tsdevice_link", func(c *gin.Context) { + deviceLink := c.Param("tsdevice_link") + + q, err := qrcode.New(deviceLink, qrcode.Medium) + if err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + } + + q.DisableBorder = true + var png []byte + png, err = q.PNG(256) + if err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + } + + c.Data(200, "image/png", png) + }) + router.Run() }