mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 14:14:47 +01:00
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/getAlby/lndhub.go/common"
|
|
"github.com/getAlby/lndhub.go/db/models"
|
|
)
|
|
|
|
func (svc *LndhubService) StartWebhookSubscribtion(ctx context.Context, url string) {
|
|
|
|
svc.Logger.Infof("Starting webhook subscription with webhook url %s", svc.Config.WebhookUrl)
|
|
incomingInvoices := make(chan models.Invoice)
|
|
outgoingInvoices := make(chan models.Invoice)
|
|
svc.InvoicePubSub.Subscribe(common.InvoiceTypeIncoming, incomingInvoices)
|
|
svc.InvoicePubSub.Subscribe(common.InvoiceTypeOutgoing, outgoingInvoices)
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case incoming := <-incomingInvoices:
|
|
svc.postToWebhook(incoming, url)
|
|
case outgoing := <-outgoingInvoices:
|
|
svc.postToWebhook(outgoing, url)
|
|
}
|
|
}
|
|
}
|
|
func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) {
|
|
|
|
payload := new(bytes.Buffer)
|
|
err := json.NewEncoder(payload).Encode(invoice)
|
|
if err != nil {
|
|
svc.Logger.Error(err)
|
|
return
|
|
}
|
|
|
|
resp, err := http.Post(svc.Config.WebhookUrl, "application/json", payload)
|
|
if err != nil {
|
|
svc.Logger.Error(err)
|
|
return
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
msg, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
svc.Logger.Error(err)
|
|
}
|
|
svc.Logger.Errorf("Webhook status code was %d, body: %s", resp.StatusCode, msg)
|
|
}
|
|
}
|