Merge pull request #38 from boltcard/updateboltcard
addition to internal API for /updateboltcard
This commit is contained in:
commit
9898ca1450
13 changed files with 136 additions and 19 deletions
|
|
@ -17,7 +17,7 @@ Environment="DB_USER=cardapp"
|
|||
Environment="DB_PASSWORD=database_password"
|
||||
Environment="DB_NAME=card_db"
|
||||
|
||||
ExecStart=/bin/bash /home/ubuntu/boltcard/s_launch
|
||||
ExecStart=/bin/bash /home/ubuntu/boltcard/script/s_launch
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
|
|
|||
37
db/db.go
37
db/db.go
|
|
@ -834,3 +834,40 @@ func Wipe_card(card_name string) (*Card_wipe_info, error) {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,10 +50,10 @@ edit `Caddyfile` to set the boltcard domain name
|
|||
### database creation
|
||||
edit `create_db.sql` to set the cardapp password
|
||||
`$ sudo -u postgres createuser -s ubuntu`
|
||||
`$ ./s_create_db`
|
||||
`$ script/s_create_db`
|
||||
|
||||
### boltcard service install
|
||||
`$ ./s_build`
|
||||
`$ script/s_build`
|
||||
`$ sudo systemctl enable boltcard`
|
||||
`$ sudo systemctl status boltcard`
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package internalapi
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
|
@ -22,7 +22,7 @@ func random_hex() string {
|
|||
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" {
|
||||
msg := "createboltcard: internal API function is not enabled"
|
||||
log.Debug(msg)
|
||||
12
internalapi/ping.go
Normal file
12
internalapi/ping.go
Normal 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)
|
||||
}
|
||||
74
internalapi/updateboltcard.go
Normal file
74
internalapi/updateboltcard.go
Normal 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)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package internalapi
|
||||
|
||||
import (
|
||||
"github.com/boltcard/boltcard/db"
|
||||
|
|
@ -8,7 +8,7 @@ import (
|
|||
"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" {
|
||||
msg := "wipeboltcard: internal API function is not enabled"
|
||||
log.Debug(msg)
|
||||
8
main.go
8
main.go
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/boltcard/boltcard/db"
|
||||
"github.com/boltcard/boltcard/internalapi"
|
||||
"github.com/boltcard/boltcard/lnurlp"
|
||||
"github.com/boltcard/boltcard/lnurlw"
|
||||
"github.com/gorilla/mux"
|
||||
|
|
@ -50,9 +51,10 @@ func main() {
|
|||
// this has no authentication and is not to be exposed publicly
|
||||
// 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("/createboltcard").Methods("GET").HandlerFunc(createboltcard)
|
||||
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(wipeboltcard)
|
||||
internal_router.Path("/ping").Methods("GET").HandlerFunc(internalapi.Internal_ping)
|
||||
internal_router.Path("/createboltcard").Methods("GET").HandlerFunc(internalapi.Createboltcard)
|
||||
internal_router.Path("/updateboltcard").Methods("GET").HandlerFunc(internalapi.Updateboltcard)
|
||||
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(internalapi.Wipeboltcard)
|
||||
|
||||
port := db.Get_setting("HOST_PORT")
|
||||
if port == "" {
|
||||
|
|
|
|||
10
ping.go
10
ping.go
|
|
@ -5,16 +5,8 @@ import (
|
|||
)
|
||||
|
||||
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.WriteHeader(http.StatusOK)
|
||||
jsonData := []byte(`{"status":"OK","pong":"` + message + `"}`)
|
||||
jsonData := []byte(`{"status":"OK","pong":"external API"}`)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue