Receive LN invoice on upload

master
Chakib Benziane 5 years ago
parent bb90656197
commit ae30315d16

@ -18,6 +18,8 @@ services:
- DB_HOST=maria
- DB_USER=bit4sat
- DB_PASS=bit4sat
- LN_CHARGE_API=10.192.122.10:9112
- LN_CHARGE_TOKEN=3emU3Fy8VasHCzMaMXHSVJYpQSqH3yXQj8N5cQFBbq3botrudJuR7zQkBBmFSbAmgXs9GD4j4U3J4R2sMfgqPo8q
#deploy:
#replicas: 1

@ -0,0 +1,124 @@
package ln
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/gin-gonic/gin"
)
const (
CurMSat = iota
CurSat
CurUSD
)
const (
LNChargeAPIEnv = "LN_CHARGE_API"
LNChargeTokenEnv = "LN_CHARGE_TOKEN"
InfoEndpoint = "info"
InvoiceEndpoint = "invoice"
)
var (
LNCEndpoint string
LNChargeToken string
)
type Charge struct {
client *http.Client
}
func NewCharge() *Charge {
netTransport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
c := &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
return &Charge{
client: c,
}
}
func (c *Charge) Info() (gin.H, error) {
result := make(gin.H)
resp, err := c.client.Get(getUrl(InfoEndpoint))
if err != nil {
return nil, err
}
jsonDec := json.NewDecoder(resp.Body)
jsonDec.Decode(&result)
return result, nil
}
func (c *Charge) Invoice(amount int, id string) (gin.H, error) {
result := make(gin.H)
reqData := gin.H{
"amount": 10,
"currency": "USD",
"description": fmt.Sprintf("bit4sat upload: %s", id),
}
jsonEnc, err := json.Marshal(reqData)
if err != nil {
return nil, err
}
resp, err := c.client.Post(getUrl(InvoiceEndpoint),
"application/json",
bytes.NewReader(jsonEnc))
if err != nil {
return nil, err
}
result = make(gin.H)
jsonDec := json.NewDecoder(resp.Body)
jsonDec.Decode(&result)
return result, nil
}
func getUrl(endpoint string) string {
url, err := url.Parse(fmt.Sprintf("http://api-token:%s@%s/%s",
LNChargeToken,
LNCEndpoint,
endpoint))
if err != nil {
log.Fatal(err)
}
return url.String()
}
func init() {
var set bool
LNCEndpoint, set = os.LookupEnv(LNChargeAPIEnv)
if !set {
log.Fatalf("%s not set", LNChargeAPIEnv)
}
LNChargeToken, set = os.LookupEnv(LNChargeTokenEnv)
if !set {
log.Fatalf("%s not set", LNChargeAPIEnv)
}
}

@ -1,9 +1,12 @@
package main
import "git.sp4ke.com/sp4ke/bit4sat/db"
import (
"git.sp4ke.com/sp4ke/bit4sat/db"
)
func main() {
defer db.DB.Sql.Close()
api := NewAPI()
api.Run()

@ -10,6 +10,7 @@ import (
"os"
"git.sp4ke.com/sp4ke/bit4sat/db"
"git.sp4ke.com/sp4ke/bit4sat/ln"
"git.sp4ke.com/sp4ke/bit4sat/utils"
"github.com/gin-gonic/gin"
"github.com/segmentio/ksuid"
@ -192,9 +193,29 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) {
return
}
// Get a new lightning invoice
charge := ln.NewCharge()
// New invoice for 10$
invoice, err := charge.Invoice(10, id)
//info, err := ln.Info()
if err != nil {
utils.JSONErrPriv(c, http.StatusInternalServerError, err)
return
}
//c.JSON(http.StatusOK, gin.H{
//"status": http.StatusOK,
//"result": fmt.Sprintf("%d files uploaded uploaded !", len(files)),
//})
result := gin.H{
"message": fmt.Sprintf("%d files uploaded uploaded !", len(files)),
"invoice": invoice,
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"result": fmt.Sprintf("%d files uploaded uploaded !", len(files)),
"result": result,
})
}

@ -108,7 +108,6 @@ func CacheSetUploadStatus(id string, status int) error {
// Returns true if id exists in DB
func IdExists(id string) (exists bool, err error) {
key := fmt.Sprintf("upload_status_%s", id)
log.Println("calling exists")
err = DB.Redis.Do(radix.Cmd(&exists, "EXISTS", key))

Loading…
Cancel
Save