REST folder restructure & cleanup

This commit is contained in:
Christopher Goes 2018-04-25 16:18:06 +02:00
parent 85a49646eb
commit 56ac948dc6
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
20 changed files with 69 additions and 59 deletions

View File

@ -18,9 +18,9 @@ import (
tx "github.com/cosmos/cosmos-sdk/client/tx" tx "github.com/cosmos/cosmos-sdk/client/tx"
version "github.com/cosmos/cosmos-sdk/version" version "github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
auth "github.com/cosmos/cosmos-sdk/x/auth/rest" auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bank "github.com/cosmos/cosmos-sdk/x/bank/rest" bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
ibc "github.com/cosmos/cosmos-sdk/x/ibc/rest" ibc "github.com/cosmos/cosmos-sdk/x/ibc/client/rest"
) )
const ( const (

View File

@ -13,10 +13,10 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/commands" stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
) )

View File

@ -14,10 +14,10 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/commands" 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/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types" "github.com/cosmos/cosmos-sdk/examples/basecoin/types"

View File

@ -14,15 +14,15 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/commands" ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
simplestakingcmd "github.com/cosmos/cosmos-sdk/x/simplestake/commands" 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/app"
"github.com/cosmos/cosmos-sdk/examples/democoin/types" "github.com/cosmos/cosmos-sdk/examples/democoin/types"
coolcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool/commands" coolcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool/client/cli"
powcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow/commands" powcmd "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow/client/cli"
) )
// rootCmd is the entry point for this binary // rootCmd is the entry point for this binary

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"fmt" "fmt"
@ -10,7 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/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/examples/democoin/x/cool" "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
) )

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"fmt" "fmt"
@ -11,7 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/examples/democoin/x/pow" "github.com/cosmos/cosmos-sdk/examples/democoin/x/pow"
"github.com/cosmos/cosmos-sdk/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"
) )
// command to mine some pow! // command to mine some pow!

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"

View File

@ -10,20 +10,21 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "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 // register REST routes
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) { func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) {
ctx := context.NewCoreContextFromViper()
r.HandleFunc( r.HandleFunc(
"/accounts/{address}", "/accounts/{address}",
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc)), QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx),
).Methods("GET") ).Methods("GET")
} }
// query accountREST Handler // query accountREST Handler
func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) func(http.ResponseWriter, *http.Request) { func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
addr := vars["address"] addr := vars["address"]

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"
@ -10,8 +10,8 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/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/bank" "github.com/cosmos/cosmos-sdk/x/bank/client"
) )
const ( const (
@ -48,7 +48,7 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command {
} }
// build and sign the transaction, then broadcast to Tendermint // 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) res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil { if err != nil {
return err return err
@ -62,11 +62,3 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command {
cmd.Flags().String(flagAmount, "", "Amount of coins to send") cmd.Flags().String(flagAmount, "", "Amount of coins to send")
return cmd 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
}

View File

@ -12,12 +12,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "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 // RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { 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 { type sendBody struct {
@ -31,8 +33,7 @@ type sendBody struct {
} }
// SendRequestHandler - http request handler to send coins to a address // SendRequestHandler - http request handler to send coins to a address
func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// collect data // collect data
vars := mux.Vars(r) vars := mux.Vars(r)
@ -68,7 +69,7 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
to := sdk.Address(bz) to := sdk.Address(bz)
// build message // 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 ? if err != nil { // XXX rechecking same error ?
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))

14
x/bank/client/util.go Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"
@ -13,7 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire" 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" "github.com/cosmos/cosmos-sdk/x/ibc"
) )

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"os" "os"
@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire" 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" "github.com/cosmos/cosmos-sdk/x/ibc"
) )

View File

@ -16,7 +16,9 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application // RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { 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 { type transferBody struct {
@ -30,8 +32,7 @@ type transferBody struct {
// TransferRequestHandler - http request handler to transfer coins to a address // TransferRequestHandler - http request handler to transfer coins to a address
// on a different chain via IBC // on a different chain via IBC
func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// collect data // collect data
vars := mux.Vars(r) vars := mux.Vars(r)

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"
@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/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/simplestake" "github.com/cosmos/cosmos-sdk/x/simplestake"
) )

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"

View File

@ -1,4 +1,4 @@
package commands package cli
import ( import (
"encoding/hex" "encoding/hex"
@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/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/stake" "github.com/cosmos/cosmos-sdk/x/stake"
) )

View File

@ -16,12 +16,13 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application // RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { 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 // BondingStatusHandler - http request handler to query delegator bonding status
func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) { func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// read parameters // read parameters
vars := mux.Vars(r) vars := mux.Vars(r)