2018-02-23 02:25:25 -08:00
|
|
|
package lcd
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
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-02-23 02:25:25 -08:00
|
|
|
|
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-02-23 02:25:25 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagBind = "bind"
|
|
|
|
flagCORS = "cors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-02-28 23:16:54 -08:00
|
|
|
RunE: startRESTServer(cdc),
|
2018-02-23 02:25:25 -08:00
|
|
|
}
|
|
|
|
// TODO: handle unix sockets also?
|
|
|
|
cmd.Flags().StringP(flagBind, "b", "localhost:1317", "Interface and port that server binds to")
|
|
|
|
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
|
|
|
|
|
|
|
func startRESTServer(cdc *wire.Codec) func(cmd *cobra.Command, args []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
bind := viper.GetString(flagBind)
|
|
|
|
r := initRouter(cdc)
|
|
|
|
return http.ListenAndServe(bind, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initRouter(cdc *wire.Codec) http.Handler {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/version", version.VersionRequestHandler).Methods("GET")
|
2018-03-09 01:15:56 -08:00
|
|
|
|
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-12 05:10:30 -07:00
|
|
|
auth.RegisterRoutes(r, cdc, "main") // TODO should use a variable not just a string
|
2018-03-10 09:33:05 -08:00
|
|
|
bank.RegisterRoutes(r, cdc)
|
2018-02-28 23:16:54 -08:00
|
|
|
return r
|
|
|
|
}
|