From be2b731d83dfde832a22fb49cf92d5f791700d4e Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 16 Apr 2018 14:50:11 -0400 Subject: [PATCH] api routes and handler --- blockExplorerApi/handler.go | 56 ++++++++++++++++++++++++++----------- blockExplorerApi/routes.go | 10 ++----- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index 6ff4229c3f..e6370e51c7 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -4,24 +4,58 @@ package main import ( "encoding/json" "fmt" + logger "log" "net/http" + "github.com/ethereum/go-ethereum/log" "github.com/gorilla/mux" "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/util" ) // GetAllTransactions gets txs func GetAllTransactions(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - address := vars["address"] - // addressBytes := []byte(address) + db, err := leveldb.OpenFile(`../shyftData/geth/blockExplorerDb/`, nil) + if err != nil { + return + } + var data []byte + iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) + for iter.Next() { - blockExplorerDb, err := leveldb.OpenFile(`../shyftData/geth/blockExplorerDb/`, nil) + logger.Printf("%s\t%s", "\nALL TX VALUE: ", string(iter.Value())) + data = append(iter.Value()) + } + + logger.Print("outside loop", data) + iter.Release() + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + json.NewEncoder(w).Encode(data) +} + +// GetBalance gets balance +func GetBalance(w http.ResponseWriter, r *http.Request) { + // vars := mux.Vars(r) + // address := vars["address"] + address := "0x43ec6d0942f7faef069f7f63d0384a27f529b062" + addressBytes := []byte(address) + + db, err := leveldb.OpenFile(`../shyftData/geth/blockExplorerDb/`, nil) if err != nil { return } - data, err := blockExplorerDb.Get([]byte(address), nil) + // if err := db.Put(addressBytes, []byte("Golang BABY WOOOOOOOOO! BARBADOS"), nil); err != nil { + // log.Crit("Failed to store TX", "err", err) + // } + + data, err := db.Get(addressBytes, nil) + if err != nil { + log.Crit("Could not retrieve block", "err", err) + } + bodyString := string(data) w.Header().Set("Content-Type", "application/json; charset=UTF-8") @@ -30,18 +64,6 @@ func GetAllTransactions(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bodyString) } -// GetBalance gets balance -func GetBalance(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - address := vars["address"] - //addressBytes := []byte(address) - - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) - - json.NewEncoder(w).Encode(address) -} - // GetBalances gets balances func GetBalances(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index da7763e37b..600092bd2c 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -18,7 +18,7 @@ var routes = Routes{ Route{ "GetBalance", "GET", - "/api/get_balance/{address}", + "/api/get_balance/", GetBalance, }, Route{ @@ -42,7 +42,7 @@ var routes = Routes{ Route{ "GetAllTransactions", "GET", - "/api/get_all_transactions/{address}", + "/api/get_all_transactions/", GetAllTransactions, }, Route{ @@ -57,10 +57,4 @@ var routes = Routes{ "/api/get_internal_transactions_hash/{transactions_hash}", GetInternalTransactionsHash, }, - // Route{ - // "PostTestData", - // "POST", - // "/api/post_test_data/", - // PostTestData, - // }, }