From a890bc98c9e8a9b106ab97c2863d6dcef34e5e77 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Jun 2015 23:15:14 -0400 Subject: main.go: Add static file server * Temporarily comment out Bitcoin transaction trigger * Add a static file server to the main function * Add a sample index.html file that gets served at the root We'll use this to serve our HTML files for the AngelHack demo. Used this article as a resource: http://www.alexedwards.net/blog/serving-static-sites-with-go --- main.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index d70d879..adc6ef7 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "com.teddywing/new-house-on-the-block/vendor/_nuts/github.com/fabioberger/coinbase-go" "fmt" "log" + "net/http" "os" ) @@ -28,10 +29,16 @@ func sendMoney(from string, to string, amount string) (transaction_id string, er } func main() { - transaction_id, err := sendMoney("TODO", "n2Qd6da1jiFgij5SSncFKh7MoFN74GdUxv", "0.0001") - if err != nil { - log.Println(err) - } else { - fmt.Println(transaction_id) - } + // transaction_id, err := sendMoney("TODO", "n2Qd6da1jiFgij5SSncFKh7MoFN74GdUxv", "0.0001") + // if err != nil { + // log.Println(err) + // } else { + // fmt.Println(transaction_id) + // } + + fs := http.FileServer(http.Dir("static")) + http.Handle("/", fs) + + log.Println("Listening on port 3000") + http.ListenAndServe(":3000", nil) } -- cgit v1.2.3 From 01186c52c83bccffad17dde5a24712b86dc3e68e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 Jun 2015 11:53:32 -0400 Subject: Move `sendMoney` func to `purchase` package Create a new package for sending money. Take API keys as args which obviously is not a good idea but it's a hackathon. * Add functions in main.go to initiate hard-coded transactions for house buyer and seller * Move from "com.teddywing" to "github.com/teddywing" --- main.go | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index adc6ef7..41f09cc 100644 --- a/main.go +++ b/main.go @@ -1,41 +1,39 @@ package main import ( - "com.teddywing/new-house-on-the-block/vendor/_nuts/github.com/fabioberger/coinbase-go" "fmt" "log" "net/http" "os" -) -func sendMoney(from string, to string, amount string) (transaction_id string, err error) { - c := coinbase.ApiKeyClientSandbox(os.Getenv("COINBASE_KEY"), os.Getenv("COINBASE_SECRET")) + "github.com/teddywing/new-house-on-the-block/purchase" +) - params := &coinbase.TransactionParams{ - To: to, - Amount: amount, - Notes: "You just bought a house", +func sendMoneyToSeller() { + transaction_id, err := purchase.SendMoney(os.Getenv("COINBASE_KEY"), + os.Getenv("COINBASE_SECRET"), + "n2Qd6da1jiFgij5SSncFKh7MoFN74GdUxv", + "0.0001") + if err != nil { + log.Println(err) + } else { + fmt.Println(transaction_id) } +} - confirmation, err := c.SendMoney(params) +func sendTokenToBuyer() { + transaction_id, err := purchase.SendMoney(os.Getenv("SELLER_COINBASE_KEY"), + os.Getenv("SELLER_COINBASE_SECRET"), + "mqy3kT6aFHymTcvmdwZLKq1Svo2m6sUtzH", + "0.0001") if err != nil { - return "", err + log.Println(err) } else { - fmt.Println(confirmation.Transaction.Status) - fmt.Println(confirmation.Transaction.Id) - - return confirmation.Transaction.Id, nil + fmt.Println(transaction_id) } } func main() { - // transaction_id, err := sendMoney("TODO", "n2Qd6da1jiFgij5SSncFKh7MoFN74GdUxv", "0.0001") - // if err != nil { - // log.Println(err) - // } else { - // fmt.Println(transaction_id) - // } - fs := http.FileServer(http.Dir("static")) http.Handle("/", fs) -- cgit v1.2.3 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(-) (limited to 'main.go') 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 From 1e78d9ef6529cabfc443d1aae933c014e0153735 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 Jun 2015 12:16:25 -0400 Subject: main.go: Add redirect endpoint for post-transaction Hard-coded URL for the demo for the next step in the process after we complete the transaction. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index a75b8b4..ef95f5c 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { log.Println(err) } - http.Redirect(w, r, "", 302) + http.Redirect(w, r, "http://testsite.perfectburpee.com/new-house-on-the-block-page6/", 302) }) log.Println("Listening on port 3000") -- cgit v1.2.3