Fn suffix & http.HandlerFunc
This commit is contained in:
+2
-2
@@ -95,7 +95,7 @@ func printBlock(cmd *cobra.Command, args []string) error {
|
||||
// REST
|
||||
|
||||
// REST handler to get a block
|
||||
func BlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func BlockRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
||||
@@ -121,7 +121,7 @@ func BlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *htt
|
||||
}
|
||||
|
||||
// REST handler to get the latest block
|
||||
func LatestBlockRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func LatestBlockRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
height, err := GetChainHeight(ctx)
|
||||
if err != nil {
|
||||
|
||||
+6
-6
@@ -47,10 +47,10 @@ func initClientCommand() *cobra.Command {
|
||||
|
||||
// Register REST endpoints
|
||||
func RegisterRoutes(ctx context.CoreContext, r *mux.Router) {
|
||||
r.HandleFunc("/node_info", NodeInfoRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/syncing", NodeSyncingRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/{height}", BlockRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandler(ctx)).Methods("GET")
|
||||
r.HandleFunc("/node_info", NodeInfoRequestHandlerFn(ctx)).Methods("GET")
|
||||
r.HandleFunc("/syncing", NodeSyncingRequestHandlerFn(ctx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/latest", LatestBlockRequestHandlerFn(ctx)).Methods("GET")
|
||||
r.HandleFunc("/blocks/{height}", BlockRequestHandlerFn(ctx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/latest", LatestValidatorSetRequestHandlerFn(ctx)).Methods("GET")
|
||||
r.HandleFunc("/validatorsets/{height}", ValidatorSetRequestHandlerFn(ctx)).Methods("GET")
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func printNodeStatus(cmd *cobra.Command, args []string) error {
|
||||
// REST
|
||||
|
||||
// REST handler for node info
|
||||
func NodeInfoRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
|
||||
func NodeInfoRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := getNodeStatus(ctx)
|
||||
if err != nil {
|
||||
@@ -73,7 +73,7 @@ func NodeInfoRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter,
|
||||
}
|
||||
|
||||
// REST handler for node syncing
|
||||
func NodeSyncingRequestHandler(ctx context.CoreContext) func(w http.ResponseWriter, r *http.Request) {
|
||||
func NodeSyncingRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := getNodeStatus(ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -73,7 +73,7 @@ func printValidators(cmd *cobra.Command, args []string) error {
|
||||
// REST
|
||||
|
||||
// Validator Set at a height REST handler
|
||||
func ValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func ValidatorSetRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
||||
@@ -99,7 +99,7 @@ func ValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWrite
|
||||
}
|
||||
|
||||
// Latest Validator Set REST handler
|
||||
func LatestValidatorSetRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func LatestValidatorSetRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
height, err := GetChainHeight(ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -13,7 +13,7 @@ type BroadcastTxBody struct {
|
||||
}
|
||||
|
||||
// BroadcastTx REST Handler
|
||||
func BroadcastTxRequestHandler(ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func BroadcastTxRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var m BroadcastTxBody
|
||||
|
||||
|
||||
+4
-4
@@ -34,7 +34,7 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
hashHexStr := args[0]
|
||||
trustNode := viper.GetBool(client.FlagTrustNode)
|
||||
|
||||
output, err := queryTx(context.NewCoreContextFromViper(), cdc, hashHexStr, trustNode)
|
||||
output, err := queryTx(cdc, context.NewCoreContextFromViper(), hashHexStr, trustNode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func queryTx(ctx context.CoreContext, cdc *wire.Codec, hashHexStr string, trustNode bool) ([]byte, error) {
|
||||
func queryTx(cdc *wire.Codec, ctx context.CoreContext, hashHexStr string, trustNode bool) ([]byte, error) {
|
||||
hash, err := hex.DecodeString(hashHexStr)
|
||||
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, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
func QueryTxRequestHandlerFn(cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
hashHexStr := vars["hash"]
|
||||
@@ -119,7 +119,7 @@ func QueryTxRequestHandler(cdc *wire.Codec, ctx context.CoreContext) func(http.R
|
||||
trustNode = true
|
||||
}
|
||||
|
||||
output, err := queryTx(ctx, cdc, hashHexStr, trustNode)
|
||||
output, err := queryTx(cdc, ctx, hashHexStr, trustNode)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
|
||||
|
||||
// register REST routes
|
||||
func RegisterRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) {
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc, ctx)).Methods("GET")
|
||||
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(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")
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error)
|
||||
// REST
|
||||
|
||||
// Search Tx REST Handler
|
||||
func SearchTxRequestHandler(ctx context.CoreContext, cdc *wire.Codec) func(http.ResponseWriter, *http.Request) {
|
||||
func SearchTxRequestHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tag := r.FormValue("tag")
|
||||
if tag == "" {
|
||||
|
||||
Reference in New Issue
Block a user