cosmos-sdk/client/rpc/status.go

94 lines
1.9 KiB
Go
Raw Normal View History

package rpc
import (
"fmt"
"net/http"
2018-03-08 05:39:59 -08:00
"strconv"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
func statusCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Query remote node for status",
RunE: printNodeStatus,
}
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
return cmd
}
func getNodeStatus(ctx context.CoreContext) (*ctypes.ResultStatus, error) {
// get the node
node, err := ctx.GetNode()
2018-02-28 15:26:39 -08:00
if err != nil {
return &ctypes.ResultStatus{}, err
2018-02-28 15:26:39 -08:00
}
return node.Status()
}
// CMD
func printNodeStatus(cmd *cobra.Command, args []string) error {
status, err := getNodeStatus(context.NewCoreContextFromViper())
if err != nil {
return err
}
2018-04-06 17:25:08 -07:00
output, err := cdc.MarshalJSON(status)
// output, err := cdc.MarshalJSONIndent(res, " ", "")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
// REST
2018-04-18 21:49:24 -07:00
// REST handler for node info
2018-04-25 13:32:22 -07:00
func NodeInfoRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(ctx)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
nodeInfo := status.NodeInfo
output, err := cdc.MarshalJSON(nodeInfo)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
}
2018-03-08 05:39:59 -08:00
2018-04-18 21:49:24 -07:00
// REST handler for node syncing
2018-04-25 13:32:22 -07:00
func NodeSyncingRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := getNodeStatus(ctx)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
2018-03-08 05:39:59 -08:00
syncing := status.SyncInfo.Syncing
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(strconv.FormatBool(syncing)))
2018-03-08 05:39:59 -08:00
}
}