Using callback no need for watchers
parent
e4ca0e2701
commit
89fc1b2894
@ -0,0 +1,80 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.sp4ke.com/sp4ke/bit4sat/bus"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/db"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/ln"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/storage"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mediocregopher/radix/v3"
|
||||
)
|
||||
|
||||
func invoiceCbHandler(c *gin.Context) {
|
||||
invoice := ln.Invoice{}
|
||||
|
||||
//dec := json.Decoder(c.Request.Body)
|
||||
|
||||
if err := c.ShouldBindJSON(&invoice); err != nil {
|
||||
log.Println(err)
|
||||
utils.JSONErr(c, http.StatusNotAcceptable,
|
||||
"callback: could not parse request from charge api")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("received invoice paid from ln charge\n%s", invoice)
|
||||
c.Status(http.StatusOK)
|
||||
|
||||
// get upload id related to invoice
|
||||
var uploadId string
|
||||
invoiceUploadKey := fmt.Sprintf("invoice_%s_upload", invoice.Id)
|
||||
err := db.DB.Redis.Do(radix.FlatCmd(&uploadId, "GET", invoiceUploadKey))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if uploadId == "" {
|
||||
log.Printf("cannot find uploadId for invoice %s", invoice)
|
||||
return
|
||||
}
|
||||
|
||||
invoice.UploadId = uploadId
|
||||
//
|
||||
// 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
|
||||
msg := bus.Message{}
|
||||
msg.Type = bus.PaymentReceived
|
||||
msg.UploadId = invoice.UploadId
|
||||
msg.Data = gin.H{
|
||||
"invoice": invoice,
|
||||
}
|
||||
|
||||
msgJson, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("Notifying upload paid %s", invoice.UploadId)
|
||||
key := fmt.Sprintf("%s_%s", bus.UploadUpdateChannelPrefix,
|
||||
invoice.UploadId)
|
||||
|
||||
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
|
||||
key, msgJson))
|
||||
|
||||
return
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
const (
|
||||
ChargeCallbackEndpoint = "chargecallback"
|
||||
)
|
||||
|
||||
var (
|
||||
StoragePath,
|
||||
ApiPort,
|
||||
ApiInterface,
|
||||
ApiListen,
|
||||
ApiHost,
|
||||
SessionSecret,
|
||||
LnChargeApi,
|
||||
LnChargeToken,
|
||||
SqlDbHost,
|
||||
SqlDbUser,
|
||||
SqlDbPass string
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Storage
|
||||
flag.StringVar(&StoragePath, "bit4sat-storage-path", "file-storage", "base file storage path")
|
||||
|
||||
// Api
|
||||
flag.StringVar(&ApiPort, "api-port", "8880", "api port number")
|
||||
flag.StringVar(&ApiInterface, "api-interface", "", "api listening interface")
|
||||
flag.StringVar(&ApiHost, "api-host", "", "reachable hostname for api (used by callbacks)")
|
||||
ApiListen = fmt.Sprintf("%s:%s", ApiInterface, ApiPort)
|
||||
|
||||
flag.StringVar(&SessionSecret, "session-secret", "default-secret", "cookie sessions secret")
|
||||
|
||||
// Database
|
||||
flag.StringVar(&SqlDbHost, "sql-db-host", "bit4sat", "sql db hostname")
|
||||
flag.StringVar(&SqlDbUser, "sql-db-user", "bit4sat", "sql db username")
|
||||
flag.StringVar(&SqlDbPass, "sql-db-pass", "bit4sat", "sql db pass")
|
||||
|
||||
// LN Charge
|
||||
flag.StringVar(&LnChargeApi, "ln-charge-api", "", "ln charge api endpoint")
|
||||
flag.StringVar(&LnChargeToken, "ln-charge-token", "", "ln charge api token")
|
||||
|
||||
flag.Parse()
|
||||
}
|
@ -1,17 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.sp4ke.com/sp4ke/bit4sat/api"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/db"
|
||||
"git.sp4ke.com/sp4ke/bit4sat/watchers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer db.DB.Sql.Close()
|
||||
|
||||
api := NewAPI()
|
||||
invoiceWatcher := watchers.NewInvoiceWatcher()
|
||||
|
||||
go invoiceWatcher.Run()
|
||||
api := api.NewAPI()
|
||||
//invoiceWatcher := watchers.NewInvoiceWatcher()
|
||||
//go invoiceWatcher.Run()
|
||||
api.Run()
|
||||
|
||||
}
|
||||
|
@ -1,24 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
SessionSecretEnv = "SESSION_SECRET"
|
||||
)
|
||||
|
||||
var (
|
||||
SessionSecret string
|
||||
)
|
||||
|
||||
func init() {
|
||||
var set bool
|
||||
|
||||
SessionSecret, set = os.LookupEnv(SessionSecretEnv)
|
||||
if !set {
|
||||
log.Fatalf("%s not set", SessionSecretEnv)
|
||||
}
|
||||
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
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/gin-gonic/gin"
|
||||
"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)
|
||||
}
|
||||
|
||||
//TODO: kill goroutine if listening websocket dies
|
||||
go ln.PollPaidInvoice(&invoice, w.invoicePaid)
|
||||
|
||||
case invoice := <-w.invoicePaid:
|
||||
log.Printf("recevied update for invoice <%s>", invoice.UploadId)
|
||||
// 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
|
||||
msg := bus.Message{}
|
||||
msg.Type = bus.PaymentReceived
|
||||
msg.UploadId = invoice.UploadId
|
||||
msg.Data = gin.H{
|
||||
"invoice": invoice,
|
||||
}
|
||||
|
||||
msgJson, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("Notifying upload paid %s", invoice.UploadId)
|
||||
key := fmt.Sprintf("%s_%s", bus.UploadUpdateChannelPrefix,
|
||||
invoice.UploadId)
|
||||
|
||||
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
|
||||
key, msgJson))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NewInvoiceWatcher() *InvoiceWatcher {
|
||||
return &InvoiceWatcher{
|
||||
listenWatchInvoice: make(chan radix.PubSubMessage),
|
||||
invoicePaid: make(chan *ln.Invoice),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue