Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding LCD route setup callbacks #1082

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ import (
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
ibc "github.com/cosmos/cosmos-sdk/x/ibc/client/rest"
ckeys "github.com/tendermint/go-crypto/keys"
)

const (
flagListenAddr = "laddr"
flagCORS = "cors"
)

// RestRegister is a callback for CLI programs to pass extra routes to the LCD server
type RestRegister func(context.CoreContext, *mux.Router, *wire.Codec, ckeys.Keybase)

// ServeCommand will generate a long-running rest server
// (aka Light Client Daemon) that exposes functionality similar
// to the cli, but over rest
func ServeCommand(cdc *wire.Codec) *cobra.Command {
func ServeCommand(cdc *wire.Codec, register ...RestRegister) *cobra.Command {
cmd := &cobra.Command{
Use: "rest-server",
Short: "Start LCD (light-client daemon), a local REST server",
RunE: startRESTServerFn(cdc),
RunE: startRESTServerFn(cdc, register...),
}
cmd.Flags().StringP(flagListenAddr, "a", "tcp://localhost:1317", "Address for server to listen on")
cmd.Flags().String(flagCORS, "", "Set to domains that can make CORS requests (* for all)")
Expand All @@ -45,10 +49,10 @@ func ServeCommand(cdc *wire.Codec) *cobra.Command {
return cmd
}

func startRESTServerFn(cdc *wire.Codec) func(cmd *cobra.Command, args []string) error {
func startRESTServerFn(cdc *wire.Codec, register ...RestRegister) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
listenAddr := viper.GetString(flagListenAddr)
handler := createHandler(cdc)
handler := createHandler(cdc, register...)
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "rest-server")
listener, err := tmserver.StartHTTPServer(listenAddr, handler, logger)
Expand All @@ -65,7 +69,7 @@ func startRESTServerFn(cdc *wire.Codec) func(cmd *cobra.Command, args []string)
}
}

func createHandler(cdc *wire.Codec) http.Handler {
func createHandler(cdc *wire.Codec, register ...RestRegister) http.Handler {
r := mux.NewRouter()
r.HandleFunc("/version", version.RequestHandler).Methods("GET")

Expand All @@ -83,5 +87,8 @@ func createHandler(cdc *wire.Codec) http.Handler {
auth.RegisterRoutes(ctx, r, cdc, "acc")
bank.RegisterRoutes(ctx, r, cdc, kb)
ibc.RegisterRoutes(ctx, r, cdc, kb)
for _, reg := range register {
reg(ctx, r, cdc, kb)
}
return r
}