mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
api routes and handler
This commit is contained in:
parent
636cba146f
commit
be2b731d83
2 changed files with 41 additions and 25 deletions
|
|
@ -4,24 +4,58 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
logger "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
"github.com/syndtr/goleveldb/leveldb/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAllTransactions gets txs
|
// GetAllTransactions gets txs
|
||||||
func GetAllTransactions(w http.ResponseWriter, r *http.Request) {
|
func GetAllTransactions(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
db, err := leveldb.OpenFile(`../shyftData/geth/blockExplorerDb/`, nil)
|
||||||
address := vars["address"]
|
if err != nil {
|
||||||
// addressBytes := []byte(address)
|
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 {
|
if err != nil {
|
||||||
return
|
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)
|
bodyString := string(data)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
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)
|
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
|
// GetBalances gets balances
|
||||||
func GetBalances(w http.ResponseWriter, r *http.Request) {
|
func GetBalances(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ var routes = Routes{
|
||||||
Route{
|
Route{
|
||||||
"GetBalance",
|
"GetBalance",
|
||||||
"GET",
|
"GET",
|
||||||
"/api/get_balance/{address}",
|
"/api/get_balance/",
|
||||||
GetBalance,
|
GetBalance,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
|
|
@ -42,7 +42,7 @@ var routes = Routes{
|
||||||
Route{
|
Route{
|
||||||
"GetAllTransactions",
|
"GetAllTransactions",
|
||||||
"GET",
|
"GET",
|
||||||
"/api/get_all_transactions/{address}",
|
"/api/get_all_transactions/",
|
||||||
GetAllTransactions,
|
GetAllTransactions,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
|
|
@ -57,10 +57,4 @@ var routes = Routes{
|
||||||
"/api/get_internal_transactions_hash/{transactions_hash}",
|
"/api/get_internal_transactions_hash/{transactions_hash}",
|
||||||
GetInternalTransactionsHash,
|
GetInternalTransactionsHash,
|
||||||
},
|
},
|
||||||
// Route{
|
|
||||||
// "PostTestData",
|
|
||||||
// "POST",
|
|
||||||
// "/api/post_test_data/",
|
|
||||||
// PostTestData,
|
|
||||||
// },
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue