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
|
|
|
"github.com/gorilla/mux"
|
2018-02-23 02:25:25 -08:00
|
|
|
"github.com/spf13/cobra"
|
2018-02-28 23:16:54 -08:00
|
|
|
"github.com/spf13/viper"
|
2018-03-14 19:16:54 -07:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2018-02-23 02:25:25 -08:00
|
|
|
|
2018-03-17 17:42:18 -07:00
|
|
|
tmserver "github.com/tendermint/tendermint/rpc/lib/server"
|
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
client "github.com/cosmos/cosmos-sdk/client"
|
|
|
|
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
|
|
|
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
|
|
|
|
tx "github.com/cosmos/cosmos-sdk/client/tx"
|
|
|
|
version "github.com/cosmos/cosmos-sdk/version"
|
2018-03-08 18:37:49 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-03-10 09:33:05 -08:00
|
|
|
auth "github.com/cosmos/cosmos-sdk/x/auth/rest"
|
|
|
|
bank "github.com/cosmos/cosmos-sdk/x/bank/rest"
|
2018-03-19 10:13:28 -07:00
|
|
|
ibc "github.com/cosmos/cosmos-sdk/x/ibc/rest"
|
2018-02-23 02:25:25 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-03-14 19:16:54 -07:00
|
|
|
flagListenAddr = "laddr"
|
|
|
|
flagCORS = "cors"
|
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-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-03-14 19:16:54 -07:00
|
|
|
RunE: startRESTServerFn(cdc),
|
2018-02-23 02:25:25 -08:00
|
|
|
}
|
2018-03-14 19:16:54 -07:00
|
|
|
cmd.Flags().StringP(flagListenAddr, "a", "tcp://localhost:1317", "Address for server to listen on")
|
2018-02-23 02:25:25 -08:00
|
|
|
cmd.Flags().String(flagCORS, "", "Set to domains that can make CORS requests (* for all)")
|
|
|
|
cmd.Flags().StringP(client.FlagChainID, "c", "", "ID of chain we connect to")
|
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
|
|
|
|
return cmd
|
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-03-14 19:16:54 -07:00
|
|
|
func startRESTServerFn(cdc *wire.Codec) func(cmd *cobra.Command, args []string) error {
|
2018-02-28 23:16:54 -08:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2018-03-14 19:16:54 -07:00
|
|
|
listenAddr := viper.GetString(flagListenAddr)
|
|
|
|
handler := createHandler(cdc)
|
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
|
|
|
|
With("module", "rest-server")
|
2018-03-17 17:42:18 -07:00
|
|
|
listener, err := tmserver.StartHTTPServer(listenAddr, handler, logger)
|
2018-03-14 19:16:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait forever and cleanup
|
|
|
|
cmn.TrapSignal(func() {
|
|
|
|
err := listener.Close()
|
|
|
|
logger.Error("Error closing listener", "err", err)
|
|
|
|
})
|
2018-03-17 17:42:18 -07:00
|
|
|
return nil
|
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()
|
|
|
|
r.HandleFunc("/version", version.VersionRequestHandler).Methods("GET")
|
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-03-12 05:10:30 -07:00
|
|
|
// TODO make more functional? aka r = keys.RegisterRoutes(r)
|
2018-03-09 01:15:56 -08:00
|
|
|
keys.RegisterRoutes(r)
|
|
|
|
rpc.RegisterRoutes(r)
|
|
|
|
tx.RegisterRoutes(r, cdc)
|
2018-03-17 17:42:18 -07:00
|
|
|
auth.RegisterRoutes(r, cdc, "main")
|
|
|
|
bank.RegisterRoutes(r, cdc, kb)
|
2018-03-19 10:13:28 -07:00
|
|
|
ibc.RegisterRoutes(r, cdc, kb)
|
2018-02-28 23:16:54 -08:00
|
|
|
return r
|
|
|
|
}
|