From 56ac948dc6949fc84d66fc92debef2e1bedd4e2e Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 25 Apr 2018 16:18:06 +0200 Subject: [PATCH] REST folder restructure & cleanup --- client/lcd/root.go | 6 +++--- cmd/gaia/cmd/gaiacli/main.go | 8 ++++---- examples/basecoin/cmd/basecli/main.go | 8 ++++---- examples/democoin/cmd/democli/main.go | 12 ++++++------ .../x/cool/{commands => client/cli}/tx.go | 4 ++-- .../x/pow/{commands => client/cli}/tx.go | 4 ++-- x/auth/{commands => client/cli}/account.go | 2 +- x/auth/{ => client}/rest/query.go | 9 +++++---- x/bank/{commands => client/cli}/sendtx.go | 16 ++++------------ x/bank/{ => client}/rest/sendtx.go | 11 ++++++----- x/bank/client/util.go | 14 ++++++++++++++ x/ibc/{commands => client/cli}/README.md | 0 x/ibc/{commands => client/cli}/ibctx.go | 4 ++-- x/ibc/{commands => client/cli}/relay.go | 4 ++-- x/ibc/{ => client}/rest/transfer.go | 7 ++++--- .../{commands => client/cli}/commands.go | 4 ++-- x/stake/{commands => client/cli}/flags.go | 2 +- x/stake/{commands => client/cli}/query.go | 2 +- x/stake/{commands => client/cli}/tx.go | 4 ++-- x/stake/{ => client}/rest/query.go | 7 ++++--- 20 files changed, 69 insertions(+), 59 deletions(-) rename examples/democoin/x/cool/{commands => client/cli}/tx.go (96%) rename examples/democoin/x/pow/{commands => client/cli}/tx.go (95%) rename x/auth/{commands => client/cli}/account.go (99%) rename x/auth/{ => client}/rest/query.go (90%) rename x/bank/{commands => client/cli}/sendtx.go (77%) rename x/bank/{ => client}/rest/sendtx.go (90%) create mode 100644 x/bank/client/util.go rename x/ibc/{commands => client/cli}/README.md (100%) rename x/ibc/{commands => client/cli}/ibctx.go (96%) rename x/ibc/{commands => client/cli}/relay.go (98%) rename x/ibc/{ => client}/rest/transfer.go (93%) rename x/simplestake/{commands => client/cli}/commands.go (96%) rename x/stake/{commands => client/cli}/flags.go (98%) rename x/stake/{commands => client/cli}/query.go (99%) rename x/stake/{commands => client/cli}/tx.go (98%) rename x/stake/{ => client}/rest/query.go (93%) diff --git a/client/lcd/root.go b/client/lcd/root.go index 81eca333e..a607504cd 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -18,9 +18,9 @@ import ( tx "github.com/cosmos/cosmos-sdk/client/tx" version "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/wire" - auth "github.com/cosmos/cosmos-sdk/x/auth/rest" - bank "github.com/cosmos/cosmos-sdk/x/bank/rest" - ibc "github.com/cosmos/cosmos-sdk/x/ibc/rest" + 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" ) const ( diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 45818b36b..8efb9f4dd 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -13,10 +13,10 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" - ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" - stakecmd "github.com/cosmos/cosmos-sdk/x/stake/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli" + stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" ) diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 6271adcec..c2a6145aa 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -14,10 +14,10 @@ import ( "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" - ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" - simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli" + simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/client/cli" "github.com/cosmos/cosmos-sdk/examples/basecoin/app" "github.com/cosmos/cosmos-sdk/examples/basecoin/types" diff --git a/examples/democoin/cmd/democli/main.go b/examples/democoin/cmd/democli/main.go index 2b71db710..534b88ca2 100644 --- a/examples/democoin/cmd/democli/main.go +++ b/examples/democoin/cmd/democli/main.go @@ -14,15 +14,15 @@ import ( "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" - ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" - simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli" + simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/client/cli" "github.com/cosmos/cosmos-sdk/examples/democoin/app" "github.com/cosmos/cosmos-sdk/examples/democoin/types" - coolcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool/commands" - powcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow/commands" + coolcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool/client/cli" + powcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow/client/cli" ) // rootCmd is the entry point for this binary diff --git a/examples/democoin/x/cool/commands/tx.go b/examples/democoin/x/cool/client/cli/tx.go similarity index 96% rename from examples/democoin/x/cool/commands/tx.go rename to examples/democoin/x/cool/client/cli/tx.go index 90f31d283..9abcfd630 100644 --- a/examples/democoin/x/cool/commands/tx.go +++ b/examples/democoin/x/cool/client/cli/tx.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool" ) diff --git a/examples/democoin/x/pow/commands/tx.go b/examples/democoin/x/pow/client/cli/tx.go similarity index 95% rename from examples/democoin/x/pow/commands/tx.go rename to examples/democoin/x/pow/client/cli/tx.go index 017047d08..62ee4db69 100644 --- a/examples/democoin/x/pow/commands/tx.go +++ b/examples/democoin/x/pow/client/cli/tx.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "fmt" @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow" "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" ) // command to mine some pow! diff --git a/x/auth/commands/account.go b/x/auth/client/cli/account.go similarity index 99% rename from x/auth/commands/account.go rename to x/auth/client/cli/account.go index 4da601df8..c6c8c6c54 100644 --- a/x/auth/commands/account.go +++ b/x/auth/client/cli/account.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" diff --git a/x/auth/rest/query.go b/x/auth/client/rest/query.go similarity index 90% rename from x/auth/rest/query.go rename to x/auth/client/rest/query.go index b97f883f9..e16884fc6 100644 --- a/x/auth/rest/query.go +++ b/x/auth/client/rest/query.go @@ -10,20 +10,21 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - auth "github.com/cosmos/cosmos-sdk/x/auth/commands" + auth "github.com/cosmos/cosmos-sdk/x/auth/client/cli" ) // register REST routes func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) { + ctx := context.NewCoreContextFromViper() + r.HandleFunc( "/accounts/{address}", - QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc)), + QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx), ).Methods("GET") } // query accountREST Handler -func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) func(http.ResponseWriter, *http.Request) { - ctx := context.NewCoreContextFromViper() +func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) addr := vars["address"] diff --git a/x/bank/commands/sendtx.go b/x/bank/client/cli/sendtx.go similarity index 77% rename from x/bank/commands/sendtx.go rename to x/bank/client/cli/sendtx.go index dbf2bb3c9..910984288 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/client/cli/sendtx.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" @@ -10,8 +10,8 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" - "github.com/cosmos/cosmos-sdk/x/bank" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + "github.com/cosmos/cosmos-sdk/x/bank/client" ) const ( @@ -48,7 +48,7 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command { } // build and sign the transaction, then broadcast to Tendermint - msg := BuildMsg(from, to, coins) + msg := client.BuildMsg(from, to, coins) res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc) if err != nil { return err @@ -62,11 +62,3 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command { cmd.Flags().String(flagAmount, "", "Amount of coins to send") return cmd } - -// build the sendTx msg -func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg { - input := bank.NewInput(from, coins) - output := bank.NewOutput(to, coins) - msg := bank.NewMsgSend([]bank.Input{input}, []bank.Output{output}) - return msg -} diff --git a/x/bank/rest/sendtx.go b/x/bank/client/rest/sendtx.go similarity index 90% rename from x/bank/rest/sendtx.go rename to x/bank/client/rest/sendtx.go index eec487a3d..83b62d03d 100644 --- a/x/bank/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -12,12 +12,14 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - "github.com/cosmos/cosmos-sdk/x/bank/commands" + "github.com/cosmos/cosmos-sdk/x/bank/client" ) // RegisterRoutes - Central function to define routes that get registered by the main application func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { - r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb)).Methods("POST") + ctx := context.NewCoreContextFromViper() + + r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb, ctx)).Methods("POST") } type sendBody struct { @@ -31,8 +33,7 @@ type sendBody struct { } // SendRequestHandler - http request handler to send coins to a address -func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { - ctx := context.NewCoreContextFromViper() +func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // collect data vars := mux.Vars(r) @@ -68,7 +69,7 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit to := sdk.Address(bz) // build message - msg := commands.BuildMsg(info.PubKey.Address(), to, m.Amount) + msg := client.BuildMsg(info.PubKey.Address(), to, m.Amount) if err != nil { // XXX rechecking same error ? w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) diff --git a/x/bank/client/util.go b/x/bank/client/util.go new file mode 100644 index 000000000..4d461b34f --- /dev/null +++ b/x/bank/client/util.go @@ -0,0 +1,14 @@ +package client + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + bank "github.com/cosmos/cosmos-sdk/x/bank" +) + +// build the sendTx msg +func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg { + input := bank.NewInput(from, coins) + output := bank.NewOutput(to, coins) + msg := bank.NewMsgSend([]bank.Input{input}, []bank.Output{output}) + return msg +} diff --git a/x/ibc/commands/README.md b/x/ibc/client/cli/README.md similarity index 100% rename from x/ibc/commands/README.md rename to x/ibc/client/cli/README.md diff --git a/x/ibc/commands/ibctx.go b/x/ibc/client/cli/ibctx.go similarity index 96% rename from x/ibc/commands/ibctx.go rename to x/ibc/client/cli/ibctx.go index 1fc53b7e6..906758dd7 100644 --- a/x/ibc/commands/ibctx.go +++ b/x/ibc/client/cli/ibctx.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" @@ -13,7 +13,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" wire "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/ibc" ) diff --git a/x/ibc/commands/relay.go b/x/ibc/client/cli/relay.go similarity index 98% rename from x/ibc/commands/relay.go rename to x/ibc/client/cli/relay.go index 6e4254926..86e8b1dbf 100644 --- a/x/ibc/commands/relay.go +++ b/x/ibc/client/cli/relay.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "os" @@ -12,7 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" wire "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/ibc" ) diff --git a/x/ibc/rest/transfer.go b/x/ibc/client/rest/transfer.go similarity index 93% rename from x/ibc/rest/transfer.go rename to x/ibc/client/rest/transfer.go index c0f70b517..9e567df01 100644 --- a/x/ibc/rest/transfer.go +++ b/x/ibc/client/rest/transfer.go @@ -16,7 +16,9 @@ import ( // RegisterRoutes - Central function to define routes that get registered by the main application func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { - r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb)).Methods("POST") + ctx := context.NewCoreContextFromViper() + + r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb, ctx)).Methods("POST") } type transferBody struct { @@ -30,8 +32,7 @@ type transferBody struct { // TransferRequestHandler - http request handler to transfer coins to a address // on a different chain via IBC -func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { - ctx := context.NewCoreContextFromViper() +func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // collect data vars := mux.Vars(r) diff --git a/x/simplestake/commands/commands.go b/x/simplestake/client/cli/commands.go similarity index 96% rename from x/simplestake/commands/commands.go rename to x/simplestake/client/cli/commands.go index 78144fdac..54ef73ac4 100644 --- a/x/simplestake/commands/commands.go +++ b/x/simplestake/client/cli/commands.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" @@ -12,7 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/simplestake" ) diff --git a/x/stake/commands/flags.go b/x/stake/client/cli/flags.go similarity index 98% rename from x/stake/commands/flags.go rename to x/stake/client/cli/flags.go index 4bafd9c65..bf8246b34 100644 --- a/x/stake/commands/flags.go +++ b/x/stake/client/cli/flags.go @@ -1,4 +1,4 @@ -package commands +package cli import ( flag "github.com/spf13/pflag" diff --git a/x/stake/commands/query.go b/x/stake/client/cli/query.go similarity index 99% rename from x/stake/commands/query.go rename to x/stake/client/cli/query.go index 3bc2cffa7..145333e48 100644 --- a/x/stake/commands/query.go +++ b/x/stake/client/cli/query.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" diff --git a/x/stake/commands/tx.go b/x/stake/client/cli/tx.go similarity index 98% rename from x/stake/commands/tx.go rename to x/stake/client/cli/tx.go index 69fedba54..188dba867 100644 --- a/x/stake/commands/tx.go +++ b/x/stake/client/cli/tx.go @@ -1,4 +1,4 @@ -package commands +package cli import ( "encoding/hex" @@ -12,7 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/stake" ) diff --git a/x/stake/rest/query.go b/x/stake/client/rest/query.go similarity index 93% rename from x/stake/rest/query.go rename to x/stake/client/rest/query.go index 240f68d96..0e148345b 100644 --- a/x/stake/rest/query.go +++ b/x/stake/client/rest/query.go @@ -16,12 +16,13 @@ import ( // RegisterRoutes - Central function to define routes that get registered by the main application func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { - r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb)).Methods("GET") + ctx := context.NewCoreContextFromViper() + + r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET") } // BondingStatusHandler - http request handler to query delegator bonding status -func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { - ctx := context.NewCoreContextFromViper() +func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // read parameters vars := mux.Vars(r)