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

[R4R]: add swagger-ui for gaiacli lite-server #2215

Merged
merged 54 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
79c0bc4
Add swagger-ui to gaiacli rest-server
Sep 2, 2018
8e032f8
Rename rest-server to lite-server and add README to clarify how to up…
Sep 2, 2018
9a1dac2
Refactor code according to code review
Sep 2, 2018
e742b02
Refactor code for key management: use cdc instead of json and remove …
Sep 2, 2018
e496bbd
Update pending and skip statik in test_lint
Sep 2, 2018
6ecec39
Reset makefile
Sep 2, 2018
093c4ad
remove useless fmt.Sprintf and add paramCheck to recover key
Sep 3, 2018
ea96d8b
Add governance and slashing, implement post /txs api
Sep 3, 2018
1e97074
Refactor key sign command and add it to gaiacli
Sep 3, 2018
ed93518
Refactor swagger.json for API docs
Sep 3, 2018
b43a1a3
Split swagger.json, currently only transaction, bank and version are …
Sep 3, 2018
76469c0
Remove responses category in swagger.json and update update_API_docs.md
Sep 7, 2018
5f38b50
modify makefile to automatically install tools and generate api docs
Sep 13, 2018
e704f09
Keep both subcommand: rest-server and lite-server
Sep 13, 2018
dafe528
rest-server is kept and there is no breaking change
Sep 13, 2018
5d5bdbb
Use http.Handler instead of mux.Router
Sep 13, 2018
69b9a53
Remove generate golang file client/lcd/statik/statik.go
Sep 13, 2018
0f29a26
Add a empty statik.go to avoid test_lint failure and add gocyclo for …
Sep 13, 2018
0c171ec
Merge branch 'develop' into bianjie/lcd-lite-swagger-ui
Sep 13, 2018
c266833
merge with develop
Sep 13, 2018
9aff862
Just for get the golang version of ci/circleci:localnet
Sep 13, 2018
5a8d823
change swaggerui to swagger-ui
Sep 13, 2018
538fd6e
Remove generated file statik.go and add empty init.go to avoid import…
Sep 14, 2018
173dccc
go fmt root.go and statik/init.go
Sep 14, 2018
0b021cf
Merge with develop
Sep 23, 2018
f472839
Remove changes in keys
Sep 23, 2018
0627675
Remove uesless code
Sep 23, 2018
dc2d98d
Now only add API doc for version
Sep 23, 2018
bd7e7b8
Add comment for broadcast type
Sep 23, 2018
bff2f33
Rename swaggerui to swagger-ui
Sep 27, 2018
c5affaa
Remove docs/clients/lcd-rest-api.yaml and add its content to client/l…
Sep 27, 2018
4e30661
Merge with develop
Sep 27, 2018
bf24a42
Remove stake API from this PR
Sep 27, 2018
6d43f9c
Correct inaccurate description
Sep 27, 2018
46e268e
Convert swagger.json to swagger.yaml
Sep 27, 2018
6e8b8e4
Add swagger-ui for ICS0 and ICS20
Sep 30, 2018
d6f6a1f
Merge with develop
Sep 30, 2018
b7f27a0
update gopkg.lock and fix compile error
Sep 30, 2018
f896497
Improve swagger.yaml
Sep 30, 2018
da5da6e
Fix lcd test failure for endpoint change
Sep 30, 2018
019b1d9
unminify JS files according to cwgoes
Sep 30, 2018
bd274db
Add application/json to response header
Oct 1, 2018
fd0f1d3
Break change, rename the endpoint to transfer tokens
Oct 1, 2018
f5df198
Add description for return type
Oct 1, 2018
5fb7e65
Remove useless comment
Oct 2, 2018
5d259b1
Add post respones process to add json to header and decide whether br…
Oct 2, 2018
9830acc
Add indent option to context and fix a bug in node status command
Oct 2, 2018
e1bb6fc
Move update_API_docs.md to docs/DOCS_README.md
Oct 2, 2018
1d4cfe8
Merge with develop
Oct 4, 2018
c676eab
WithTrustNode, Makefile fixes, 'make format'
cwgoes Oct 4, 2018
bf3e276
Update CircleCI Golang container
cwgoes Oct 4, 2018
e72c19a
Revert viper.Set change
cwgoes Oct 4, 2018
d46b562
Merge pull request #8 from cosmos/cwgoes/patch-fix
Oct 4, 2018
31b26e7
Refactor code in set response header
Oct 4, 2018
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
defaults: &defaults
working_directory: /go/src/github.com/cosmos/cosmos-sdk
docker:
- image: circleci/golang:1.10.3
- image: circleci/golang:1.11.1
environment:
GOBIN: /tmp/workspace/bin

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ docs/_build
examples/basecoin/app/data
baseapp/data/*
client/lcd/keys/*
client/lcd/statik/statik.go
mytestnet

# Testing
Expand Down
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
name = "github.com/zondax/ledger-goclient"
version = "=v0.1.0"

[[constraint]]
name = "github.com/rakyll/statik"
version = "=v0.1.4"

[prune]
go-tests = true
unused-packages = true
Expand Down
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TMP_BUILD_TAGS := $(BUILD_TAGS)
BUILD_TAGS = $(filter-out ledger, $(TMP_BUILD_TAGS))
endif

build: check-ledger
build: check-ledger update_gaia_lite_docs
ifeq ($(OS),Windows_NT)
go build $(BUILD_FLAGS) -o build/gaiad.exe ./cmd/gaia/cmd/gaiad
go build $(BUILD_FLAGS) -o build/gaiacli.exe ./cmd/gaia/cmd/gaiacli
Expand All @@ -44,6 +44,9 @@ endif
build-linux:
LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build

update_gaia_lite_docs:
@statik -src=client/lcd/swagger-ui -dest=client/lcd -f

build_cosmos-sdk-cli:
ifeq ($(OS),Windows_NT)
go build $(BUILD_FLAGS) -o build/cosmos-sdk-cli.exe ./cmd/cosmos-sdk-cli
Expand All @@ -64,7 +67,7 @@ else
go build $(BUILD_FLAGS) -o build/democli ./examples/democoin/cmd/democli
endif

install: check-ledger
install: check-ledger update_gaia_lite_docs
go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiad
go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiacli

Expand Down Expand Up @@ -179,14 +182,14 @@ test_cover:

test_lint:
gometalinter.v2 --config=tools/gometalinter.json ./...
!(gometalinter.v2 --exclude /usr/lib/go/src/ --disable-all --enable='errcheck' --vendor ./... | grep -v "client/")
!(gometalinter.v2 --exclude /usr/lib/go/src/ --exclude client/lcd/statik/statik.go --disable-all --enable='errcheck' --vendor ./... | grep -v "client/")
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s
dep status >> /dev/null
!(grep -n branch Gopkg.toml)

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/lcd/statik/statik.go" | xargs misspell -w

benchmark:
@go test -bench=. $(PACKAGES_NOSIMULATION)
Expand Down
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ FEATURES
* [gaia-lite] [\#966](https://github.com/cosmos/cosmos-sdk/issues/966) Add support for `generate_only=true` query argument to generate offline unsigned transactions
* [gaia-lite] [\#1953](https://github.com/cosmos/cosmos-sdk/issues/1953) Add /sign endpoint to sign transactions generated with `generate_only=true`.
* [gaia-lite] [\#1954](https://github.com/cosmos/cosmos-sdk/issues/1954) Add /broadcast endpoint to broadcast transactions signed by the /sign endpoint.
* [gaia-lite] [\#2113](https://github.com/cosmos/cosmos-sdk/issues/2113) Rename `/accounts/{address}/send` to `/bank/accounts/{address}/transfers`

* Gaia CLI (`gaiacli`)
* [cli] Cmds to query staking pool and params
Expand Down
16 changes: 16 additions & 0 deletions client/context/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ func (ctx CLIContext) BroadcastTxAndAwaitCommit(tx []byte) (*ctypes.ResultBroadc
return res, err
}

// BroadcastTxSync broadcasts transaction bytes to a Tendermint node
// synchronously.
func (ctx CLIContext) BroadcastTxSync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
}

res, err := node.BroadcastTxSync(tx)
if err != nil {
return res, err
}

return res, err
}

// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
// asynchronously.
func (ctx CLIContext) BroadcastTxAsync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
Expand Down
2 changes: 2 additions & 0 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CLIContext struct {
GenerateOnly bool
fromAddress types.AccAddress
fromName string
Indent bool
}

// NewCLIContext returns a new initialized CLIContext with parameters from the
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewCLIContext() CLIContext {
GenerateOnly: viper.GetBool(client.FlagGenerateOnly),
fromAddress: fromAddress,
fromName: fromName,
Indent: viper.GetBool(client.FlagIndentResponse),
}
}

Expand Down
39 changes: 21 additions & 18 deletions client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ const (
DefaultGasLimit = 200000
GasFlagSimulate = "simulate"

FlagUseLedger = "ledger"
FlagChainID = "chain-id"
FlagNode = "node"
FlagHeight = "height"
FlagGas = "gas"
FlagGasAdjustment = "gas-adjustment"
FlagTrustNode = "trust-node"
FlagFrom = "from"
FlagName = "name"
FlagAccountNumber = "account-number"
FlagSequence = "sequence"
FlagMemo = "memo"
FlagFee = "fee"
FlagAsync = "async"
FlagJson = "json"
FlagPrintResponse = "print-response"
FlagDryRun = "dry-run"
FlagGenerateOnly = "generate-only"
FlagUseLedger = "ledger"
FlagChainID = "chain-id"
FlagNode = "node"
FlagHeight = "height"
FlagGas = "gas"
FlagGasAdjustment = "gas-adjustment"
FlagTrustNode = "trust-node"
FlagFrom = "from"
FlagName = "name"
FlagAccountNumber = "account-number"
FlagSequence = "sequence"
FlagMemo = "memo"
FlagFee = "fee"
FlagAsync = "async"
FlagJson = "json"
FlagPrintResponse = "print-response"
FlagDryRun = "dry-run"
FlagGenerateOnly = "generate-only"
FlagIndentResponse = "indent"
)

// LineBreak can be included in a command list to provide a blank line
Expand All @@ -47,6 +48,7 @@ var (
// GetCommands adds common flags to query commands
func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().Bool(FlagIndentResponse, false, "Add indent to JSON response")
c.Flags().Bool(FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device")
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
Expand All @@ -63,6 +65,7 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
// PostCommands adds common flags for commands to post tx
func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().Bool(FlagIndentResponse, false, "Add indent to JSON response")
c.Flags().String(FlagFrom, "", "Name or address of private key with which to sign")
c.Flags().Int64(FlagAccountNumber, 0, "AccountNumber number to sign the tx")
c.Flags().Int64(FlagSequence, 0, "Sequence number to sign the tx")
Expand Down
7 changes: 4 additions & 3 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {

// broadcast tx
broadcastPayload := struct {
Tx auth.StdTx `json:"tx"`
}{Tx: signedMsg}
Tx auth.StdTx `json:"tx"`
Return string `json:"return"`
}{Tx: signedMsg, Return: "block"}
json, err = cdc.MarshalJSON(broadcastPayload)
require.Nil(t, err)
res, body = Request(t, port, "POST", "/tx/broadcast", json)
Expand Down Expand Up @@ -851,7 +852,7 @@ func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.Acc
}
}`, coinbz, gasStr, gasAdjustmentStr, name, password, chainID, accnum, sequence))

res, body = Request(t, port, "POST", fmt.Sprintf("/accounts/%s/send%v", receiveAddr, queryStr), jsonStr)
res, body = Request(t, port, "POST", fmt.Sprintf("/bank/accounts/%s/transfers%v", receiveAddr, queryStr), jsonStr)
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
14 changes: 13 additions & 1 deletion client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
stake "github.com/cosmos/cosmos-sdk/x/stake/client/rest"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmn "github.com/tendermint/tendermint/libs/common"
Expand Down Expand Up @@ -46,6 +47,7 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) (err error) {
listenAddr := viper.GetString(flagListenAddr)
handler := createHandler(cdc)
registerSwaggerUI(handler)
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "rest-server")
maxOpen := viper.GetInt(flagMaxOpenConnections)
sslHosts := viper.GetString(flagSSLHosts)
Expand Down Expand Up @@ -121,14 +123,15 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command {
cmd.Flags().String(client.FlagNode, "tcp://localhost:26657", "Address of the node to connect to")
cmd.Flags().Int(flagMaxOpenConnections, 1000, "The number of maximum open connections")
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
cmd.Flags().Bool(client.FlagIndentResponse, false, "Add indent to JSON response")
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID))
viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode))

return cmd
}

func createHandler(cdc *codec.Codec) http.Handler {
func createHandler(cdc *codec.Codec) *mux.Router {
r := mux.NewRouter()

kb, err := keys.GetKeyBase() //XXX
Expand All @@ -154,6 +157,15 @@ func createHandler(cdc *codec.Codec) http.Handler {
return r
}

func registerSwaggerUI(r *mux.Router) {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
r.PathPrefix("/swagger-ui/").Handler(http.StripPrefix("/swagger-ui/", staticServer))
}

func validateCertKeyFiles(certFile, keyFile string) error {
if keyFile == "" {
return errors.New("a key file is required")
Expand Down
3 changes: 3 additions & 0 deletions client/lcd/statik/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package statik

//This just for fixing the error in importing empty github.com/cosmos/cosmos-sdk/client/lcd/statik
Binary file added client/lcd/swagger-ui/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/lcd/swagger-ui/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions client/lcd/swagger-ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="swagger-ui.css" >
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>

<script src="swagger-ui-bundle.js"> </script>
<script src="swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {

// Build a system
const ui = SwaggerUIBundle({
url: "./swagger.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui
}
</script>
</body>
</html>
Loading