lightningtip/lightningtip.go

133 lines
2.3 KiB
Go
Raw Normal View History

2018-03-22 11:03:54 -07:00
package main
2018-03-24 15:03:55 -07:00
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
)
type invoiceResponse struct {
Invoice string
2018-03-25 08:45:42 -07:00
Expiry int64
2018-03-24 15:03:55 -07:00
}
2018-03-25 08:45:42 -07:00
type invoiceRequest struct {
Amount int64
Message string
2018-03-24 15:03:55 -07:00
}
type errorResponse struct {
Error string
}
2018-03-23 16:08:03 -07:00
2018-03-25 08:45:42 -07:00
// TODO: add option to show URI of Lightning node
2018-03-22 11:03:54 -07:00
func main() {
initLog()
2018-03-22 13:52:16 -07:00
initConfig()
2018-03-23 16:08:03 -07:00
err := backend.Connect()
if err == nil {
2018-03-24 15:03:55 -07:00
http.HandleFunc("/", notFoundHandler)
http.HandleFunc("/getinvoice", getInvoiceHandler)
log.Info("Subscribing to invoices")
go func() {
2018-03-25 08:45:42 -07:00
// TODO: let clients listen if their invoice was paid (eventsource)
2018-03-24 15:03:55 -07:00
err = cfg.LND.SubscribeInvoices()
if err != nil {
log.Error("Failed to subscribe to invoices: " + fmt.Sprint(err))
os.Exit(1)
}
}()
log.Info("Starting HTTP server")
go func() {
err = http.ListenAndServe(cfg.RESTHost, nil)
if err != nil {
log.Error("Failed to start HTTP server: " + fmt.Sprint(err))
os.Exit(1)
}
}()
select {}
}
}
func getInvoiceHandler(writer http.ResponseWriter, request *http.Request) {
2018-03-25 08:45:42 -07:00
errorMessage := "Could not parse values from request"
2018-03-24 15:03:55 -07:00
if request.Method == http.MethodPost {
2018-03-25 08:45:42 -07:00
var body invoiceRequest
2018-03-24 15:03:55 -07:00
data, _ := ioutil.ReadAll(request.Body)
err := json.Unmarshal(data, &body)
if err == nil {
2018-03-25 08:45:42 -07:00
if body.Amount != 0 {
invoice, err := backend.GetInvoice(body.Message, body.Amount, cfg.TipExpiry)
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
if err == nil {
logMessage := "Created invoice with amount of " + strconv.FormatInt(body.Amount, 10) + " satoshis"
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
if body.Message != "" {
logMessage += " with message \"" + body.Message + "\""
}
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
log.Info(logMessage)
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
writer.Write(marshalJson(invoiceResponse{
Invoice: invoice,
Expiry: cfg.TipExpiry,
}))
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
return
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
} else {
errorMessage = "Failed to create invoice"
}
2018-03-23 16:08:03 -07:00
2018-03-25 08:45:42 -07:00
}
2018-03-23 16:08:03 -07:00
2018-03-25 08:45:42 -07:00
}
2018-03-24 15:03:55 -07:00
2018-03-23 16:08:03 -07:00
}
2018-03-25 08:45:42 -07:00
log.Error(errorMessage)
2018-03-24 15:03:55 -07:00
2018-03-25 08:45:42 -07:00
writeError(writer, errorMessage)
2018-03-24 15:03:55 -07:00
}
func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
2018-03-25 08:45:42 -07:00
writeError(writer, "Not found")
}
func writeError(writer http.ResponseWriter, message string) {
writer.WriteHeader(http.StatusBadRequest)
2018-03-24 15:03:55 -07:00
writer.Write(marshalJson(errorResponse{
2018-03-25 08:45:42 -07:00
Error: message,
2018-03-24 15:03:55 -07:00
}))
}
func marshalJson(data interface{}) []byte {
response, _ := json.MarshalIndent(data, "", " ")
return response
}