Render request + qr code

This commit is contained in:
Amarpreet Minhas 2023-05-05 01:15:09 -04:00
parent c58314a948
commit e043ca1c69
2 changed files with 34 additions and 2 deletions

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<p>Nostr NIP-05 {{ .RequestKey }}</p><br>
<img src="data:image/png;base64,{{.QRCode}}"/><br>
</div>
</body>
</html>

View file

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -33,6 +34,11 @@ type NostrRequest struct {
Relays []string `json:"relays,omitempty"` Relays []string `json:"relays,omitempty"`
} }
type NostrResponse struct {
RequestKey string `json:"request_key"`
QRCode string `json:"QRCode"`
}
func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
ctx := context.TODO() ctx := context.TODO()
l := logger.Get() l := logger.Get()
@ -99,6 +105,7 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
// generate qr png // generate qr png
png, err := qrcode.Encode(paymentReq, qrcode.Medium, 256) png, err := qrcode.Encode(paymentReq, qrcode.Medium, 256)
pngB64 := b64.StdEncoding.EncodeToString(png)
// write request to redis // write request to redis
err = redisCli.Set(ctx, rKey, enc, 15*time.Minute).Err() err = redisCli.Set(ctx, rKey, enc, 15*time.Minute).Err()
@ -107,9 +114,22 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
} }
// return qr code // return qr code
w.Header().Set("Content-Type", "image/png") response := NostrResponse{
RequestKey: rKey,
QRCode: pngB64,
}
tmplt, err := template.ParseFiles("html/nostr_qr_response.html")
if err != nil {
l.Error().Msg("unable to parse template")
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(png) err = tmplt.Execute(w, response)
if err != nil {
l.Error().Msg("unable to connect to render template")
}
return
} }
} }