Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e30a4ecae | ||
|
|
8ddb6c2e74 |
+35
-10
@@ -142,21 +142,46 @@ type InvoiceStream struct {
|
|||||||
|
|
||||||
func (s Service) InvoiceStream(w http.ResponseWriter, r *http.Request) {
|
func (s Service) InvoiceStream(w http.ResponseWriter, r *http.Request) {
|
||||||
user := telegram.LoadUser(r.Context())
|
user := telegram.LoadUser(r.Context())
|
||||||
w.Header().Set("Content-Type", "application/stream+json")
|
flusher, ok := w.(http.Flusher)
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
if !ok {
|
||||||
flusher, err := w.(http.Flusher)
|
|
||||||
if !err {
|
|
||||||
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client := sse.NewClient(fmt.Sprintf("%s/api/v1/payments/sse", internal.Configuration.Lnbits.Url))
|
client := sse.NewClient(fmt.Sprintf("%s/api/v1/payments/sse", internal.Configuration.Lnbits.Url))
|
||||||
|
client.Connection.Transport = &http.Transport{DisableCompression: true}
|
||||||
client.Headers = map[string]string{"X-Api-Key": user.Wallet.Inkey}
|
client.Headers = map[string]string{"X-Api-Key": user.Wallet.Inkey}
|
||||||
client.Subscribe("", func(msg *sse.Event) {
|
c := make(chan *sse.Event)
|
||||||
if msg.Data != nil {
|
err := client.SubscribeChan("", c)
|
||||||
fmt.Fprintf(w, "%s\n", string(msg.Data))
|
if err != nil {
|
||||||
flusher.Flush()
|
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-c:
|
||||||
|
select {
|
||||||
|
case <-r.Context().Done():
|
||||||
|
client.Unsubscribe(c)
|
||||||
|
break out
|
||||||
|
default:
|
||||||
|
written, err := fmt.Fprintf(w, "event: %s\n", string(msg.Event))
|
||||||
|
if err != nil || written == 0 {
|
||||||
|
break out
|
||||||
|
}
|
||||||
|
written, err = fmt.Fprintf(w, "data: %s\n", string(msg.Data))
|
||||||
|
if err != nil || written == 0 {
|
||||||
|
break out
|
||||||
|
}
|
||||||
|
written, err = fmt.Fprint(w, "\n")
|
||||||
|
if err != nil || written == 0 {
|
||||||
|
break out
|
||||||
|
}
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
}
|
||||||
|
close(c)
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user