From 1b4c3530aee05547fd6341ac206223dfaf1c2a7b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 Jun 2015 12:03:34 -0400 Subject: main.go: Create an HTTP handler to initiate transactions Visiting this endpoint will create 2 transactions that sends the money. Handle errors in the HTTP handler and pass them back from the transaction functions now. --- main.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 41f09cc..a75b8b4 100644 --- a/main.go +++ b/main.go @@ -9,27 +9,29 @@ import ( "github.com/teddywing/new-house-on-the-block/purchase" ) -func sendMoneyToSeller() { +func sendMoneyToSeller() error { transaction_id, err := purchase.SendMoney(os.Getenv("COINBASE_KEY"), os.Getenv("COINBASE_SECRET"), "n2Qd6da1jiFgij5SSncFKh7MoFN74GdUxv", "0.0001") if err != nil { - log.Println(err) + return err } else { fmt.Println(transaction_id) + return nil } } -func sendTokenToBuyer() { +func sendTokenToBuyer() error { transaction_id, err := purchase.SendMoney(os.Getenv("SELLER_COINBASE_KEY"), os.Getenv("SELLER_COINBASE_SECRET"), "mqy3kT6aFHymTcvmdwZLKq1Svo2m6sUtzH", "0.0001") if err != nil { - log.Println(err) + return err } else { fmt.Println(transaction_id) + return nil } } @@ -37,6 +39,22 @@ func main() { fs := http.FileServer(http.Dir("static")) http.Handle("/", fs) + http.HandleFunc("/buy/", func(w http.ResponseWriter, r *http.Request) { + var err error + + err = sendMoneyToSeller() + if err != nil { + log.Println(err) + } + + err = sendTokenToBuyer() + if err != nil { + log.Println(err) + } + + http.Redirect(w, r, "", 302) + }) + log.Println("Listening on port 3000") http.ListenAndServe(":3000", nil) } -- cgit v1.2.3