implemented invoice watcher
parent
bcd3160cfd
commit
c1900d52a3
@ -0,0 +1,18 @@
|
|||||||
|
package bus
|
||||||
|
|
||||||
|
// PubSub Update Types
|
||||||
|
const (
|
||||||
|
UpdatePaymentReceived = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
// Channel names
|
||||||
|
const (
|
||||||
|
WatchInvoicesChannelName = "watch_invoices"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Simple message used for update notifications in pub/sub
|
||||||
|
type UpdateMessage struct {
|
||||||
|
UploadId string
|
||||||
|
Status int // update type
|
||||||
|
Data interface{}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package watchers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.sp4ke.com/sp4ke/bit4sat/bus"
|
||||||
|
"git.sp4ke.com/sp4ke/bit4sat/db"
|
||||||
|
"git.sp4ke.com/sp4ke/bit4sat/ln"
|
||||||
|
"git.sp4ke.com/sp4ke/bit4sat/storage"
|
||||||
|
"github.com/mediocregopher/radix/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InvoiceWatcher struct {
|
||||||
|
listenWatchInvoice chan radix.PubSubMessage
|
||||||
|
invoicePaid chan *ln.Invoice
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *InvoiceWatcher) Run() {
|
||||||
|
if err := db.DB.RedisPubSub.Subscribe(w.listenWatchInvoice,
|
||||||
|
bus.WatchInvoicesChannelName); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-w.listenWatchInvoice:
|
||||||
|
log.Printf("checking new invoice %s", msg.Message)
|
||||||
|
|
||||||
|
invoice := ln.Invoice{}
|
||||||
|
err := json.Unmarshal(msg.Message, &invoice)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go ln.PollPaidInvoice(&invoice, w.invoicePaid)
|
||||||
|
|
||||||
|
case invoice := <-w.invoicePaid:
|
||||||
|
// Set upload status to UpWaitingStorage (paid)
|
||||||
|
err := storage.SetUploadStatus(invoice.UploadId, storage.UpWaitingStorage)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Upload Invoice
|
||||||
|
err = storage.SetUploadInvoice(invoice.UploadId, invoice)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// publish invoice was updated to upload_id_paid channel
|
||||||
|
updateMsg := bus.UpdateMessage{}
|
||||||
|
updateMsg.Status = bus.UpdatePaymentReceived
|
||||||
|
updateMsg.UploadId = invoice.UploadId
|
||||||
|
updateMsg.Data = invoice
|
||||||
|
|
||||||
|
updateMsgJson, err := json.Marshal(updateMsg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Notifying upload %s paid", invoice.UploadId)
|
||||||
|
key := fmt.Sprintf("upload_update_%s", invoice.UploadId)
|
||||||
|
|
||||||
|
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
|
||||||
|
key, updateMsgJson))
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInvoiceWatcher() *InvoiceWatcher {
|
||||||
|
return &InvoiceWatcher{
|
||||||
|
listenWatchInvoice: make(chan radix.PubSubMessage),
|
||||||
|
invoicePaid: make(chan *ln.Invoice),
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,22 @@
|
|||||||
|
#172.29.195.31
|
||||||
localhost
|
localhost
|
||||||
|
|
||||||
root dist
|
root dist
|
||||||
ext .html
|
ext .html
|
||||||
|
|
||||||
header / {
|
header / {
|
||||||
Content-Security-Policy "default-src * 'unsafe-eval' 'unsafe-inline' data:;"
|
Content-Security-Policy "default-src * 'unsafe-eval' 'unsafe-inline' data: ;"
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy /api localhost:8880
|
proxy /api localhost:8880
|
||||||
|
|
||||||
log /api stdout
|
#log /api stdout
|
||||||
|
log / stdout
|
||||||
|
|
||||||
proxy /ws localhost:8880 {
|
proxy /ws localhost:8880 {
|
||||||
websocket
|
websocket
|
||||||
transparent
|
transparent
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy /wss localhost:8880
|
|
||||||
|
#tls self_signed
|
||||||
|
Loading…
Reference in New Issue