Merge pull request #38 from boltcard/updateboltcard

addition to internal API for /updateboltcard
This commit is contained in:
Peter Rounce 2023-02-22 12:41:11 +00:00 committed by GitHub
commit 9898ca1450
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 136 additions and 19 deletions

View file

@ -17,7 +17,7 @@ Environment="DB_USER=cardapp"
Environment="DB_PASSWORD=database_password" Environment="DB_PASSWORD=database_password"
Environment="DB_NAME=card_db" Environment="DB_NAME=card_db"
ExecStart=/bin/bash /home/ubuntu/boltcard/s_launch ExecStart=/bin/bash /home/ubuntu/boltcard/script/s_launch
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View file

@ -834,3 +834,40 @@ func Wipe_card(card_name string) (*Card_wipe_info, error) {
return &card_wipe_info, nil return &card_wipe_info, nil
} }
func Update_card(card_name string, tx_limit_sats int, lnurlw_enable bool) error {
lnurlw_enable_yn := "N"
if lnurlw_enable {
lnurlw_enable_yn = "Y"
}
db, err := open()
if err != nil {
return err
}
defer db.Close()
sqlStatement := `UPDATE cards SET tx_limit_sats = $2, lnurlw_enable = $3 ` +
`WHERE card_name = $1;`
res, err := db.Exec(sqlStatement, card_name, tx_limit_sats, lnurlw_enable_yn)
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
if count != 1 {
return errors.New("not one card record updated")
}
return nil
}

View file

@ -50,10 +50,10 @@ edit `Caddyfile` to set the boltcard domain name
### database creation ### database creation
edit `create_db.sql` to set the cardapp password edit `create_db.sql` to set the cardapp password
`$ sudo -u postgres createuser -s ubuntu` `$ sudo -u postgres createuser -s ubuntu`
`$ ./s_create_db` `$ script/s_create_db`
### boltcard service install ### boltcard service install
`$ ./s_build` `$ script/s_build`
`$ sudo systemctl enable boltcard` `$ sudo systemctl enable boltcard`
`$ sudo systemctl status boltcard` `$ sudo systemctl status boltcard`

View file

@ -1,4 +1,4 @@
package main package internalapi
import ( import (
"crypto/rand" "crypto/rand"
@ -22,7 +22,7 @@ func random_hex() string {
return hex.EncodeToString(b) return hex.EncodeToString(b)
} }
func createboltcard(w http.ResponseWriter, r *http.Request) { func Createboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" { if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "createboltcard: internal API function is not enabled" msg := "createboltcard: internal API function is not enabled"
log.Debug(msg) log.Debug(msg)

12
internalapi/ping.go Normal file
View file

@ -0,0 +1,12 @@
package internalapi
import (
"net/http"
)
func Internal_ping(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK","pong":"internal API"}`)
w.Write(jsonData)
}

View file

@ -0,0 +1,74 @@
package internalapi
import (
"github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/resp_err"
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
)
func Updateboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "updateboltcard: internal API function is not enabled"
log.Debug(msg)
resp_err.Write_message(w, msg)
return
}
tx_limit_sats_str := r.URL.Query().Get("tx_limit_sats")
tx_limit_sats, err := strconv.Atoi(tx_limit_sats_str)
if err != nil {
msg := "updateboltcard: tx_limit_sats is not a valid integer"
log.Warn(msg)
resp_err.Write_message(w, msg)
return
}
enable_flag_str := r.URL.Query().Get("enable")
enable_flag, err := strconv.ParseBool(enable_flag_str)
if err != nil {
msg := "updateboltcard: enable is not a valid boolean"
log.Warn(msg)
resp_err.Write_message(w, msg)
return
}
card_name := r.URL.Query().Get("card_name")
// check if card_name exists
card_count, err := db.Get_card_name_count(card_name)
if err != nil {
log.Warn(err.Error())
return
}
if card_count == 0 {
msg := "updateboltcard: the card name does not exist in the database"
log.Warn(msg)
resp_err.Write_message(w, msg)
return
}
// log the request
log.WithFields(log.Fields{
"card_name": card_name, "tx_limit_sats": tx_limit_sats,
"enable": enable_flag}).Info("updateboltcard API request")
// update the card record
err = db.Update_card(card_name, tx_limit_sats, enable_flag)
if err != nil {
log.Warn(err.Error())
return
}
// send a response
jsonData := []byte(`{"status":"OK"}`)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
}

View file

@ -1,4 +1,4 @@
package main package internalapi
import ( import (
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
@ -8,7 +8,7 @@ import (
"strconv" "strconv"
) )
func wipeboltcard(w http.ResponseWriter, r *http.Request) { func Wipeboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" { if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "wipeboltcard: internal API function is not enabled" msg := "wipeboltcard: internal API function is not enabled"
log.Debug(msg) log.Debug(msg)

View file

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/internalapi"
"github.com/boltcard/boltcard/lnurlp" "github.com/boltcard/boltcard/lnurlp"
"github.com/boltcard/boltcard/lnurlw" "github.com/boltcard/boltcard/lnurlw"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -50,9 +51,10 @@ func main() {
// this has no authentication and is not to be exposed publicly // this has no authentication and is not to be exposed publicly
// it exists for use on a private virtual network within a docker container // it exists for use on a private virtual network within a docker container
internal_router.Path("/ping").Methods("GET").HandlerFunc(internal_ping) internal_router.Path("/ping").Methods("GET").HandlerFunc(internalapi.Internal_ping)
internal_router.Path("/createboltcard").Methods("GET").HandlerFunc(createboltcard) internal_router.Path("/createboltcard").Methods("GET").HandlerFunc(internalapi.Createboltcard)
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(wipeboltcard) internal_router.Path("/updateboltcard").Methods("GET").HandlerFunc(internalapi.Updateboltcard)
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(internalapi.Wipeboltcard)
port := db.Get_setting("HOST_PORT") port := db.Get_setting("HOST_PORT")
if port == "" { if port == "" {

10
ping.go
View file

@ -5,16 +5,8 @@ import (
) )
func external_ping(w http.ResponseWriter, req *http.Request) { func external_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "external API")
}
func internal_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "internal API")
}
func ping(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK","pong":"` + message + `"}`) jsonData := []byte(`{"status":"OK","pong":"external API"}`)
w.Write(jsonData) w.Write(jsonData)
} }