refactored route registering

This commit is contained in:
Fabian 2018-03-09 10:15:56 +01:00 committed by Ethan Buchman
parent afea775748
commit 2f470baf19
7 changed files with 110 additions and 81 deletions

View File

@ -2,6 +2,7 @@ package keys
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
)
@ -27,3 +28,12 @@ func Commands() *cobra.Command {
)
return cmd
}
func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/keys", QueryKeysRequestHandler).Methods("GET")
r.HandleFunc("/keys", AddNewKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/seed", SeedRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", GetKeyRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", UpdateKeyRequestHandler).Methods("PUT")
r.HandleFunc("/keys/{name}", DeleteKeyRequestHandler).Methods("DELETE")
}

View File

@ -48,21 +48,9 @@ func startRESTServer(cdc *wire.Codec) func(cmd *cobra.Command, args []string) er
func initRouter(cdc *wire.Codec) http.Handler {
r := mux.NewRouter()
r.HandleFunc("/version", version.VersionRequestHandler).Methods("GET")
r.HandleFunc("/node_info", rpc.NodeInfoRequestHandler).Methods("GET")
r.HandleFunc("/syncing", rpc.NodeSyncingRequestHandler).Methods("GET")
r.HandleFunc("/keys", keys.QueryKeysRequestHandler).Methods("GET")
r.HandleFunc("/keys", keys.AddNewKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/seed", keys.SeedRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", keys.GetKeyRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", keys.UpdateKeyRequestHandler).Methods("PUT")
r.HandleFunc("/keys/{name}", keys.DeleteKeyRequestHandler).Methods("DELETE")
r.HandleFunc("/txs", tx.SearchTxRequestHandler(cdc)).Methods("GET")
r.HandleFunc("/txs/{hash}", tx.QueryTxRequestHandler(cdc)).Methods("GET")
r.HandleFunc("/txs/sign", tx.SignTxRequstHandler).Methods("POST")
r.HandleFunc("/txs/broadcast", tx.BroadcastTxRequestHandler).Methods("POST")
r.HandleFunc("/blocks/latest", rpc.LatestBlockRequestHandler).Methods("GET")
r.HandleFunc("/blocks/{height}", rpc.BlockRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/latest", rpc.LatestValidatorsetRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/{height}", rpc.ValidatorsetRequestHandler).Methods("GET")
keys.RegisterRoutes(r)
rpc.RegisterRoutes(r)
tx.RegisterRoutes(r, cdc)
return r
}

View File

@ -1,6 +1,7 @@
package rpc
import (
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -42,3 +43,12 @@ func initClientCommand() *cobra.Command {
cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)")
return cmd
}
func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/node_info", NodeInfoRequestHandler).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandler).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler).Methods("GET")
r.HandleFunc("/blocks/{height}", BlockRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/latest", LatestValidatorsetRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorsetRequestHandler).Methods("GET")
}

33
client/tx/broadcast.go Normal file
View File

@ -0,0 +1,33 @@
package tx
import (
"encoding/json"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
)
type BroadcastTxBody struct {
TxBytes string `json="tx"`
}
func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
var m BroadcastTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
res, err := client.BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(string(res.Height)))
}

View File

@ -12,11 +12,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
abci "github.com/tendermint/abci/types"
keys "github.com/tendermint/go-crypto/keys"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
keybase "github.com/cosmos/cosmos-sdk/client/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
@ -132,66 +130,3 @@ func QueryTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Requ
w.Write(output)
}
}
// TODO refactor into different files show, sign, broadcast
type SignTxBody struct {
Name string `json="name"`
Password string `json="password"`
TxBytes string `json="tx"`
}
func SignTxRequstHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m SignTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
kb, err = keybase.GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
//TODO check if account exists
sig, _, err := kb.Sign(m.Name, m.Password, []byte(m.TxBytes))
if err != nil {
w.WriteHeader(403)
w.Write([]byte(err.Error()))
return
}
w.Write(sig.Bytes())
}
type BroadcastTxBody struct {
TxBytes string `json="tx"`
}
func BroadcastTxRequestHandler(w http.ResponseWriter, r *http.Request) {
var m BroadcastTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
res, err := client.BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(string(res.Height)))
}

View File

@ -1,6 +1,7 @@
package tx
import (
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/wire"
@ -19,3 +20,10 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
QueryTxCmd(cmdr),
)
}
func RegisterRoutes(r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET")
r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc)).Methods("GET")
r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")
}

45
client/tx/sign.go Normal file
View File

@ -0,0 +1,45 @@
package tx
import (
"encoding/json"
"net/http"
keybase "github.com/cosmos/cosmos-sdk/client/keys"
keys "github.com/tendermint/go-crypto/keys"
)
type SignTxBody struct {
Name string `json="name"`
Password string `json="password"`
TxBytes string `json="tx"`
}
func SignTxRequstHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m SignTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
kb, err = keybase.GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
//TODO check if account exists
sig, _, err := kb.Sign(m.Name, m.Password, []byte(m.TxBytes))
if err != nil {
w.WriteHeader(403)
w.Write([]byte(err.Error()))
return
}
w.Write(sig.Bytes())
}