Only one CoreContext for all REST commands
This commit is contained in:
+19
-17
@@ -13,23 +13,25 @@ type BroadcastTxBody struct {
|
||||
}
|
||||
|
||||
// BroadcastTx REST Handler
|
||||
func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var m BroadcastTxBody
|
||||
func BroadcastTxRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var m BroadcastTxBody
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&m)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&m)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
res, err := ctx.BroadcastTx([]byte(m.TxBytes))
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(string(res.Height)))
|
||||
}
|
||||
|
||||
res, err := context.NewCoreContextFromViper().BroadcastTx([]byte(m.TxBytes))
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(string(res.Height)))
|
||||
}
|
||||
|
||||
+5
-5
@@ -34,7 +34,7 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
hashHexStr := args[0]
|
||||
trustNode := viper.GetBool(client.FlagTrustNode)
|
||||
|
||||
output, err := queryTx(cdc, hashHexStr, trustNode)
|
||||
output, err := queryTx(context.NewCoreContextFromViper(), cdc, hashHexStr, trustNode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -51,14 +51,14 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func queryTx(cdc *wire.Codec, hashHexStr string, trustNode bool) ([]byte, error) {
|
||||
func queryTx(ctx context.CoreContext, cdc *wire.Codec, hashHexStr string, trustNode bool) ([]byte, error) {
|
||||
hash, err := hex.DecodeString(hashHexStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get the node
|
||||
node, err := context.NewCoreContextFromViper().GetNode()
|
||||
node, err := ctx.GetNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -109,7 +109,7 @@ func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) {
|
||||
// REST
|
||||
|
||||
// transaction query REST handler
|
||||
func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
||||
func QueryTxRequestHandler(cdc *wire.Codec, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
hashHexStr := vars["hash"]
|
||||
@@ -119,7 +119,7 @@ func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Requ
|
||||
trustNode = true
|
||||
}
|
||||
|
||||
output, err := queryTx(cdc, hashHexStr, trustNode)
|
||||
output, err := queryTx(ctx, cdc, hashHexStr, trustNode)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
|
||||
}
|
||||
|
||||
// register REST routes
|
||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec) {
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc)).Methods("GET")
|
||||
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) {
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc, ctx)).Methods("GET")
|
||||
// r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET")
|
||||
// r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
|
||||
// r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")
|
||||
|
||||
+5
-5
@@ -29,7 +29,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
tags := viper.GetStringSlice(flagTags)
|
||||
|
||||
output, err := searchTx(cdc, tags)
|
||||
output, err := searchTx(context.NewCoreContextFromViper(), cdc, tags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func searchTx(cdc *wire.Codec, tags []string) ([]byte, error) {
|
||||
func searchTx(ctx context.CoreContext, cdc *wire.Codec, tags []string) ([]byte, error) {
|
||||
if len(tags) == 0 {
|
||||
return nil, errors.New("Must declare at least one tag to search")
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func searchTx(cdc *wire.Codec, tags []string) ([]byte, error) {
|
||||
query := strings.Join(tags, " AND ")
|
||||
|
||||
// get the node
|
||||
node, err := context.NewCoreContextFromViper().GetNode()
|
||||
node, err := ctx.GetNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error)
|
||||
// REST
|
||||
|
||||
// Search Tx REST Handler
|
||||
func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
||||
func SearchTxRequestHandler(ctx context.CoreContext, cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tag := r.FormValue("tag")
|
||||
if tag == "" {
|
||||
@@ -104,7 +104,7 @@ func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Req
|
||||
}
|
||||
|
||||
tags := []string{tag}
|
||||
output, err := searchTx(cdc, tags)
|
||||
output, err := searchTx(ctx, cdc, tags)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
|
||||
Reference in New Issue
Block a user