diff --git a/blockExplorerApi/app.go b/blockExplorerApi/app.go new file mode 100644 index 0000000000..c1334fe903 --- /dev/null +++ b/blockExplorerApi/app.go @@ -0,0 +1,16 @@ +package main + +//@NOTE SHYFT main func for api, sets up router and spins up a server +//to run server 'go run blockExplorerApi/*.go' +import ( + "log" + "net/http" + + "github.com/gorilla/handlers" +) + +func main() { + + router := NewRouter() + log.Fatal(http.ListenAndServe(":8080", handlers.CORS(handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}), handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"}), handlers.AllowedOrigins([]string{"*"}))(router))) +} diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go new file mode 100644 index 0000000000..6ff4229c3f --- /dev/null +++ b/blockExplorerApi/handler.go @@ -0,0 +1,98 @@ +package main + +///@NOTE Shyft handler functions when endpoints are hit +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/gorilla/mux" + "github.com/syndtr/goleveldb/leveldb" +) + +// GetAllTransactions gets txs +func GetAllTransactions(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + address := vars["address"] + // addressBytes := []byte(address) + + blockExplorerDb, err := leveldb.OpenFile(`../shyftData/geth/blockExplorerDb/`, nil) + if err != nil { + return + } + + data, err := blockExplorerDb.Get([]byte(address), nil) + bodyString := string(data) + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + 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) + addresses := vars["addresses"] + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + fmt.Fprintln(w, "Get Balances", addresses) +} + +// GetBlocksMined get blocks mined +func GetBlocksMined(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + address := vars["address"] + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + fmt.Fprintln(w, "Get Blocks Mined", address) +} + +// GetTransactions gets txs +func GetTransactions(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + address := vars["address"] + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + fmt.Fprintln(w, "Get Transactions", address) +} + +//GetInternalTransactions gets internal txs +func GetInternalTransactions(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + address := vars["address"] + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + fmt.Fprintln(w, "GetInternalTransactions", address) +} + +//GetInternalTransactionsHash gets internal txs hash +func GetInternalTransactionsHash(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + transactionHash := vars["transaction_hash"] + + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + + fmt.Fprintln(w, "Get Internal Transaction Hash", transactionHash) +} diff --git a/blockExplorerApi/logger.go b/blockExplorerApi/logger.go new file mode 100644 index 0000000000..dbeeaa8a94 --- /dev/null +++ b/blockExplorerApi/logger.go @@ -0,0 +1,25 @@ +package main + +//@ NOTE Shyft logs responses in terminal +import ( + "log" + "net/http" + "time" +) + +//Logger logs responses to terminal +func Logger(inner http.Handler, name string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + inner.ServeHTTP(w, r) + + log.Printf( + "%s\t%s\t%s\t%s", + r.Method, + r.RequestURI, + name, + time.Since(start), + ) + }) +} diff --git a/blockExplorerApi/router.go b/blockExplorerApi/router.go new file mode 100644 index 0000000000..e25dee9bd8 --- /dev/null +++ b/blockExplorerApi/router.go @@ -0,0 +1,27 @@ +package main + +//@ NOTE Shyft setting up router +import ( + "net/http" + + "github.com/gorilla/mux" +) + +//NewRouter sets up router +func NewRouter() *mux.Router { + + router := mux.NewRouter().StrictSlash(true) + for _, route := range routes { + var handler http.Handler + + handler = route.HandlerFunc + handler = Logger(handler, route.Name) + + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(handler) + } + return router +} diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go new file mode 100644 index 0000000000..da7763e37b --- /dev/null +++ b/blockExplorerApi/routes.go @@ -0,0 +1,66 @@ +package main + +//@NOTE Shyft setting up endpoints +import "net/http" + +//Route stuct +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +//Routes routes +type Routes []Route + +var routes = Routes{ + Route{ + "GetBalance", + "GET", + "/api/get_balance/{address}", + GetBalance, + }, + Route{ + "GetBalances", + "GET", + "/api/get_balances/{addresses}", + GetBalances, + }, + Route{ + "GetBlocksMined", + "GET", + "/api/get_blocks_mined/{address}", + GetBlocksMined, + }, + Route{ + "GetTransactions", + "GET", + "/api/get_transactions/{address}", + GetTransactions, + }, + Route{ + "GetAllTransactions", + "GET", + "/api/get_all_transactions/{address}", + GetAllTransactions, + }, + Route{ + "GetInternalTransactions", + "GET", + "/api/get_internal_transactions/{address}", + GetInternalTransactions, + }, + Route{ + "GetInternalTransactionsHash", + "GET", + "/api/get_internal_transactions_hash/{transactions_hash}", + GetInternalTransactionsHash, + }, + // Route{ + // "PostTestData", + // "POST", + // "/api/post_test_data/", + // PostTestData, + // }, +}