2018-02-23 02:25:25 -08:00
|
|
|
package lcd
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-03-14 19:16:54 -07:00
|
|
|
"os"
|
2018-02-23 02:25:25 -08:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
client "github.com/cosmos/cosmos-sdk/client"
|
2018-04-25 07:49:31 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-02-28 23:16:54 -08:00
|
|
|
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
|
|
|
|
tx "github.com/cosmos/cosmos-sdk/client/tx"
|
2018-03-08 18:37:49 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-04-25 07:18:06 -07:00
|
|
|
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
|
|
|
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
|
2018-06-21 17:19:14 -07:00
|
|
|
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
|
2018-04-25 07:18:06 -07:00
|
|
|
ibc "github.com/cosmos/cosmos-sdk/x/ibc/client/rest"
|
2018-06-29 20:34:55 -07:00
|
|
|
slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
|
2018-05-14 16:49:26 -07:00
|
|
|
stake "github.com/cosmos/cosmos-sdk/x/stake/client/rest"
|
2018-07-09 11:52:24 -07:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmserver "github.com/tendermint/tendermint/rpc/lib/server"
|
2018-02-23 02:25:25 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServeCommand will generate a long-running rest server
|
|
|
|
// (aka Light Client Daemon) that exposes functionality similar
|
|
|
|
// to the cli, but over rest
|
2018-02-28 23:16:54 -08:00
|
|
|
func ServeCommand(cdc *wire.Codec) *cobra.Command {
|
2018-06-11 18:12:37 -07:00
|
|
|
flagListenAddr := "laddr"
|
|
|
|
flagCORS := "cors"
|
2018-06-28 17:54:47 -07:00
|
|
|
flagMaxOpenConnections := "max-open"
|
2018-06-11 18:12:37 -07:00
|
|
|
|
2018-02-23 02:25:25 -08:00
|
|
|
cmd := &cobra.Command{
|
2018-02-28 23:16:54 -08:00
|
|
|
Use: "rest-server",
|
2018-02-23 02:25:25 -08:00
|
|
|
Short: "Start LCD (light-client daemon), a local REST server",
|
2018-06-11 18:12:37 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
listenAddr := viper.GetString(flagListenAddr)
|
|
|
|
handler := createHandler(cdc)
|
2018-07-09 11:52:24 -07:00
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "rest-server")
|
2018-06-28 17:54:47 -07:00
|
|
|
maxOpen := viper.GetInt(flagMaxOpenConnections)
|
2018-07-09 11:52:24 -07:00
|
|
|
|
|
|
|
listener, err := tmserver.StartHTTPServer(
|
|
|
|
listenAddr, handler, logger,
|
|
|
|
tmserver.Config{MaxOpenConnections: maxOpen},
|
|
|
|
)
|
2018-06-11 18:12:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-09 11:52:24 -07:00
|
|
|
|
2018-06-11 18:12:37 -07:00
|
|
|
logger.Info("REST server started")
|
|
|
|
|
2018-07-09 11:52:24 -07:00
|
|
|
// wait forever and cleanup
|
2018-06-11 18:12:37 -07:00
|
|
|
cmn.TrapSignal(func() {
|
|
|
|
err := listener.Close()
|
2018-06-13 12:13:22 -07:00
|
|
|
logger.Error("error closing listener", "err", err)
|
2018-06-11 18:12:37 -07:00
|
|
|
})
|
2018-07-09 11:52:24 -07:00
|
|
|
|
2018-06-11 18:12:37 -07:00
|
|
|
return nil
|
|
|
|
},
|
2018-02-23 02:25:25 -08:00
|
|
|
}
|
2018-07-09 11:52:24 -07:00
|
|
|
|
|
|
|
cmd.Flags().String(flagListenAddr, "tcp://localhost:1317", "The address for the server to listen on")
|
|
|
|
cmd.Flags().String(flagCORS, "", "Set the domains that can make CORS requests (* for all)")
|
|
|
|
cmd.Flags().String(client.FlagChainID, "", "The chain ID to connect to")
|
|
|
|
cmd.Flags().String(client.FlagNode, "tcp://localhost:26657", "Address of the node to connect to")
|
|
|
|
cmd.Flags().Int(flagMaxOpenConnections, 1000, "The number of maximum open connections")
|
|
|
|
|
2018-02-23 02:25:25 -08:00
|
|
|
return cmd
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-03-14 19:16:54 -07:00
|
|
|
func createHandler(cdc *wire.Codec) http.Handler {
|
2018-02-28 23:16:54 -08:00
|
|
|
r := mux.NewRouter()
|
2018-03-09 01:15:56 -08:00
|
|
|
|
2018-03-17 17:42:18 -07:00
|
|
|
kb, err := keys.GetKeyBase() //XXX
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc).WithLogger(os.Stdout)
|
2018-04-25 07:49:31 -07:00
|
|
|
|
2018-07-09 11:52:24 -07:00
|
|
|
// TODO: make more functional? aka r = keys.RegisterRoutes(r)
|
2018-06-13 22:49:21 -07:00
|
|
|
r.HandleFunc("/version", CLIVersionRequestHandler).Methods("GET")
|
2018-08-06 11:11:30 -07:00
|
|
|
r.HandleFunc("/node_version", NodeVersionRequestHandler(cliCtx)).Methods("GET")
|
2018-07-09 11:52:24 -07:00
|
|
|
|
2018-03-09 01:15:56 -08:00
|
|
|
keys.RegisterRoutes(r)
|
2018-08-06 11:11:30 -07:00
|
|
|
rpc.RegisterRoutes(cliCtx, r)
|
|
|
|
tx.RegisterRoutes(cliCtx, r, cdc)
|
|
|
|
auth.RegisterRoutes(cliCtx, r, cdc, "acc")
|
|
|
|
bank.RegisterRoutes(cliCtx, r, cdc, kb)
|
|
|
|
ibc.RegisterRoutes(cliCtx, r, cdc, kb)
|
|
|
|
stake.RegisterRoutes(cliCtx, r, cdc, kb)
|
|
|
|
slashing.RegisterRoutes(cliCtx, r, cdc, kb)
|
|
|
|
gov.RegisterRoutes(cliCtx, r, cdc)
|
2018-07-09 11:52:24 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
return r
|
|
|
|
}
|