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