mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 01:43:47 +00:00
api skeleton
This commit is contained in:
parent
aa9af18947
commit
7da7319f4e
5 changed files with 232 additions and 0 deletions
16
blockExplorerApi/app.go
Normal file
16
blockExplorerApi/app.go
Normal file
|
|
@ -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)))
|
||||
}
|
||||
98
blockExplorerApi/handler.go
Normal file
98
blockExplorerApi/handler.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
25
blockExplorerApi/logger.go
Normal file
25
blockExplorerApi/logger.go
Normal file
|
|
@ -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),
|
||||
)
|
||||
})
|
||||
}
|
||||
27
blockExplorerApi/router.go
Normal file
27
blockExplorerApi/router.go
Normal file
|
|
@ -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
|
||||
}
|
||||
66
blockExplorerApi/routes.go
Normal file
66
blockExplorerApi/routes.go
Normal file
|
|
@ -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,
|
||||
// },
|
||||
}
|
||||
Loading…
Reference in a new issue