Last active
September 9, 2023 16:19
-
-
Save mayankchoubey/c9a43ef81174aa39371bbb49ca1ab463 to your computer and use it in GitHub Desktop.
Go - QR generator API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| qrcode "github.com/skip2/go-qrcode" | |
| ) | |
| type QrRequest struct { | |
| UrlToEmbed string `json:"urlToEmbed"` | |
| } | |
| func requestHandler(c *gin.Context) { | |
| var qrRequest QrRequest | |
| if err := c.BindJSON(&qrRequest); err != nil { | |
| c.AbortWithStatus(http.StatusBadRequest) | |
| return | |
| } | |
| if qrRequest.UrlToEmbed == "" { | |
| c.AbortWithStatus(http.StatusBadRequest) | |
| return | |
| } | |
| var qrCode []byte | |
| qrCode, err := qrcode.Encode(qrRequest.UrlToEmbed, qrcode.Medium, 512) | |
| if err != nil { | |
| c.AbortWithStatus(http.StatusInternalServerError) | |
| return | |
| } | |
| c.Data(http.StatusOK, "image/png", qrCode) | |
| } | |
| func main() { | |
| r := gin.New() | |
| r.POST("/qr", requestHandler) | |
| r.RunTLS(":3000", | |
| "/Users/mayankc/Work/source/certs/cert.pem", | |
| "/Users/mayankc/Work/source/certs/key.pem") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment