diff --git a/lnd/lnd.go b/lnd/lnd.go index 2d5e77e..5678979 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -38,8 +38,6 @@ func Request(rKey string) (string, error) { } expiryInMin := int64(5) - expiryTime := time.Now().Local().Add(time.Minute * time.Duration(expiryInMin)).Unix() - invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{ Memo: fmt.Sprintf("nostr addr %s", rKey), Expiry: expiryInMin * 60, @@ -49,6 +47,10 @@ func Request(rKey string) (string, error) { l.Fatal().Msg("unable to create lnd invoice") return "", errors.New("internal server error") } + + // add a minute to the expiry time to ensure entries aren't removed from redis before they are expired + expiryTime := time.Now().Local().Add(time.Minute * time.Duration(expiryInMin+1)).Unix() + paymentReq := invoice.PaymentRequest rHash := hex.EncodeToString(invoice.RHash) diff --git a/nostr/nostr.go b/nostr/nostr.go index 1bdcdaf..59a4a2a 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -43,6 +43,7 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { l := logger.Get() switch r.Method { case "GET": + w.WriteHeader(http.StatusOK) http.ServeFile(w, r, "html/nostr_form.html") case "POST": r.ParseForm() @@ -110,6 +111,11 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { err = redisCli.Set(ctx, rKey, enc, 0).Err() if err != nil { l.Error().Msg("unable to connect to redis") + w.WriteHeader(http.StatusServiceUnavailable) + json.NewEncoder(w).Encode(&jsonErrorMessage{ + Error: "service unavailable", + }) + return } // return qr code @@ -121,12 +127,20 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { tmplt, err := template.ParseFiles("html/nostr_qr_response.html") if err != nil { l.Error().Msg("unable to parse template") + w.WriteHeader(http.StatusServiceUnavailable) + json.NewEncoder(w).Encode(&jsonErrorMessage{ + Error: "service unavailable", + }) + return } - w.Header().Set("Content-Type", "text/html") - w.WriteHeader(http.StatusOK) err = tmplt.Execute(w, response) if err != nil { l.Error().Msg("unable to connect to render template") + w.WriteHeader(http.StatusServiceUnavailable) + json.NewEncoder(w).Encode(&jsonErrorMessage{ + Error: "service unavailable", + }) + return } return }