Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docker/card/db/db_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Db_select_card_payments(db_conn *sql.DB, card_id int) (result CardPayments)
var cardPayments CardPayments

sqlStatement := `SELECT card_payment_id,` +
` amount_sats, paid_flag,` +
` amount_sats, fee_sats, paid_flag,` +
` timestamp, expire_time` +
` FROM card_payments` +
` WHERE card_payments.card_id = $1` +
Expand All @@ -96,6 +96,7 @@ func Db_select_card_payments(db_conn *sql.DB, card_id int) (result CardPayments)
err := rows.Scan(
&cardPayment.CardPaymentId,
&cardPayment.AmountSats,
&cardPayment.FeeSats,
&cardPayment.IsPaid,
&cardPayment.Timestamp,
&cardPayment.ExpireTime)
Expand Down
63 changes: 63 additions & 0 deletions docker/card/phoenix/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package phoenix

import (
"errors"
"fmt"
"io"
"net/http"
"time"

"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)

const phoenixBaseURL = "http://phoenix:9740"
const defaultTimeout = 5 * time.Second

// loadPassword reads the http-password from the phoenix config file.
func loadPassword() (string, error) {
cfg, err := ini.Load("/root/.phoenix/phoenix.conf")
if err != nil {
return "", fmt.Errorf("load phoenix config: %w", err)
}
return cfg.Section("").Key("http-password").String(), nil
}

// doRequest executes an HTTP request against the Phoenix API with basic auth.
// It returns the response body bytes on success, or an error on failure.
func doRequest(req *http.Request, timeout time.Duration, endpointName string) ([]byte, error) {
password, err := loadPassword()
if err != nil {
return nil, err
}

req.SetBasicAuth("", password)

client := http.Client{Timeout: timeout}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%s request failed: %w", endpointName, err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("%s read body: %w", endpointName, err)
}

if res.StatusCode != 200 {
log.Warning(endpointName, " StatusCode ", res.StatusCode)
return nil, errors.New("failed API call to Phoenix " + endpointName)
}

return body, nil
}

// doGet is a convenience wrapper for GET requests with the default timeout.
func doGet(path string, endpointName string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, phoenixBaseURL+path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("%s create request: %w", endpointName, err)
}
return doRequest(req, defaultTimeout, endpointName)
}
46 changes: 11 additions & 35 deletions docker/card/phoenix/create_invoice.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package phoenix

import (
"card/util"

"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)

Expand All @@ -28,51 +22,33 @@ type CreateInvoiceResponse struct {
}

func CreateInvoice(createInvoiceRequest CreateInvoiceRequest) (CreateInvoiceResponse, error) {

var createInvoiceResponse CreateInvoiceResponse

cfg, err := ini.Load("/root/.phoenix/phoenix.conf")
util.CheckAndPanic(err)

hp := cfg.Section("").Key("http-password").String()

client := http.Client{Timeout: 5 * time.Second}

formBody := url.Values{
"description": []string{createInvoiceRequest.Description},
"amountSat": []string{createInvoiceRequest.AmountSat},
"externalId": []string{createInvoiceRequest.ExternalId},
}
dataReader := formBody.Encode()
reader := strings.NewReader(dataReader)
reader := strings.NewReader(formBody.Encode())

req, err := http.NewRequest(http.MethodPost, "http://phoenix:9740/createinvoice", reader)
req, err := http.NewRequest(http.MethodPost, phoenixBaseURL+"/createinvoice", reader)
if err != nil {
log.Fatal(err)
return createInvoiceResponse, err
}

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

req.SetBasicAuth("", hp)

res, err := client.Do(req)
util.CheckAndPanic(err)

defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
util.CheckAndPanic(err)

if res.StatusCode != 200 {
log.Warning("CreateInvoice StatusCode ", res.StatusCode)
log.Warning(string(resBody))
return createInvoiceResponse, errors.New("failed API call to Phoenix CreateInvoice")
body, err := doRequest(req, defaultTimeout, "CreateInvoice")
if err != nil {
return createInvoiceResponse, err
}

log.Info(string(resBody))
log.Info(string(body))

err = json.Unmarshal(resBody, &createInvoiceResponse)
util.CheckAndPanic(err)
err = json.Unmarshal(body, &createInvoiceResponse)
if err != nil {
return createInvoiceResponse, err
}

return createInvoiceResponse, nil
}
41 changes: 2 additions & 39 deletions docker/card/phoenix/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ package phoenix

import (
"encoding/json"
"errors"
"io"
"net/http"
"time"

"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)

type Balance struct {
Expand All @@ -19,42 +12,12 @@ type Balance struct {
func GetBalance() (Balance, error) {
var balance Balance

cfg, err := ini.Load("/root/.phoenix/phoenix.conf")
if err != nil {
return balance, err
}

hp := cfg.Section("").Key("http-password").String()

client := http.Client{Timeout: 5 * time.Second}

req, err := http.NewRequest(http.MethodGet, "http://phoenix:9740/getbalance", http.NoBody)
if err != nil {
return balance, err
}

req.SetBasicAuth("", hp)

res, err := client.Do(req)
if err != nil {
return balance, err
}

defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
body, err := doGet("/getbalance", "GetBalance")
if err != nil {
return balance, err
}

if res.StatusCode != 200 {
log.Warning("GetBalance StatusCode ", res.StatusCode)
return balance, errors.New("failed API call to Phoenix GetBalance")
}

//log.Info(string(resBody))

err = json.Unmarshal(resBody, &balance)
err = json.Unmarshal(body, &balance)
if err != nil {
return balance, err
}
Expand Down
44 changes: 6 additions & 38 deletions docker/card/phoenix/get_incoming_payment.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package phoenix

import (
"card/util"

"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)

type IncomingPayment struct {
Expand All @@ -29,41 +20,18 @@ type IncomingPayment struct {
}

func GetIncomingPayment(PaymentHash string) (IncomingPayment, error) {

var incomingPayment IncomingPayment

cfg, err := ini.Load("/root/.phoenix/phoenix.conf")
util.CheckAndPanic(err)

hp := cfg.Section("").Key("http-password").String()

client := http.Client{Timeout: 5 * time.Second}

url := fmt.Sprintf("http://phoenix:9740/payments/incoming/%s", url.QueryEscape(PaymentHash))
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
path := fmt.Sprintf("/payments/incoming/%s", url.QueryEscape(PaymentHash))
body, err := doGet(path, "GetIncomingPayment")
if err != nil {
log.Fatal(err)
return incomingPayment, err
}

req.SetBasicAuth("", hp)

res, err := client.Do(req)
util.CheckAndPanic(err)

defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
util.CheckAndPanic(err)

if res.StatusCode != 200 {
log.Warning("GetIncomingPayment StatusCode ", res.StatusCode)
return incomingPayment, errors.New("failed API call to Phoenix GetIncomingPayment")
err = json.Unmarshal(body, &incomingPayment)
if err != nil {
return incomingPayment, err
}

//log.Info(string(resBody))

err = json.Unmarshal(resBody, &incomingPayment)
util.CheckAndPanic(err)

return incomingPayment, nil
}
41 changes: 2 additions & 39 deletions docker/card/phoenix/get_node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ package phoenix

import (
"encoding/json"
"errors"
"io"
"net/http"
"time"

"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)

type NodeInfo struct {
Expand All @@ -27,42 +20,12 @@ type NodeInfo struct {
func GetNodeInfo() (NodeInfo, error) {
var nodeInfo NodeInfo

cfg, err := ini.Load("/root/.phoenix/phoenix.conf")
if err != nil {
return nodeInfo, err
}

hp := cfg.Section("").Key("http-password").String()

client := http.Client{Timeout: 5 * time.Second}

req, err := http.NewRequest(http.MethodGet, "http://phoenix:9740/getinfo", http.NoBody)
if err != nil {
return nodeInfo, err
}

req.SetBasicAuth("", hp)

res, err := client.Do(req)
if err != nil {
return nodeInfo, err
}

defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
body, err := doGet("/getinfo", "GetNodeInfo")
if err != nil {
return nodeInfo, err
}

if res.StatusCode != 200 {
log.Warning("GetNodeInfo StatusCode ", res.StatusCode)
return nodeInfo, errors.New("failed API call to Phoenix GetNodeInfo")
}

//log.Info(string(resBody))

err = json.Unmarshal(resBody, &nodeInfo)
err = json.Unmarshal(body, &nodeInfo)
if err != nil {
return nodeInfo, err
}
Expand Down
Loading