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

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

merged 54 commits into from
Oct 4, 2018

Conversation

HaoyangLiu
Copy link

@HaoyangLiu HaoyangLiu commented Sep 2, 2018

This is another prototype of swagger-ui implementation for gaia-lite server. Comparing with previous one #2199, this implementation brings in much less code change and easy to maintain.

The following files under client/lcd/swaggerui are copied from the swagger-api

favicon-16x16.png
favicon-32x32.png
index.html
oauth2-redirect.html
swagger-ui-bundle.js
swagger-ui-bundle.js.map
swagger-ui.css
swagger-ui.css.map
swagger-ui.js
swagger-ui.js.map
swagger-ui-standalone-preset.js
swagger-ui-standalone-preset.js.map

Besides, client/lcd/statik/statik.go is generated by the tool statik.

If we want to update API docs, we just need to follow the steps edit client/lcd/swaggerui/swagger.json.
Run the following command to start gaia-lite:

gaiacli lite-server --chain-id=<chain-id>

Then you can visit this url to check your modified API docs:

http://localhost:1317/swaggerui/

@jackzampolin @fedekunze
Do you think this is acceptable? If not please give me some suggestions. Thanks very much.

}

// RecoverResuestHandler is the handler of creating seed in swagger rest server
func RecoverResuestHandler(w http.ResponseWriter, r *http.Request) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you're refering to Rest or Request with Resuest, please change it accordingly

decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use http library with the corresponding status codes constants

name := vars["name"]
var m RecoverKeyBody

decoder := json.NewDecoder(r.Body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use wire Codec to decode the body

```
go get github.com/rakyll/statik
```
* Directly Edit API docs: client/lcd/swaggerui/swagger.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link to file from develop branch

```
* Directly Edit API docs: client/lcd/swaggerui/swagger.json

* Edit API docs within this [website](https://app.swaggerhub.com). Please refer to this [link](https://app.swaggerhub.com/help/index) for how to use the about website to edit API docs.
Copy link
Collaborator

@fedekunze fedekunze Sep 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

within this [website] [SwaggerHub]


* Edit API docs within this [website](https://app.swaggerhub.com). Please refer to this [link](https://app.swaggerhub.com/help/index) for how to use the about website to edit API docs.

* Download swagger.json and replace the old swagger.json under client/lcd/swaggerui folds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Download swagger.json and replace the old swagger.json under client/lcd/swaggerui folder


* Download swagger.json and replace the old swagger.json under client/lcd/swaggerui folds

* Regenerate statik.go file
Copy link
Collaborator

@fedekunze fedekunze Sep 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate a new statik.go file with the following command:

// TODO Do we really need this endpoint?
res, body := Request(t, port, "GET", "/keys/seed", nil)
// recover key
recoverKeyURL := fmt.Sprintf("/keys/%s/recover", "test_recover")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move /keys/{name}/recover logic to a separate function ? Same way as getAccount, doSend, etc. are implemented

@@ -18,6 +18,7 @@ import (
slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"

_ "github.com/cosmos/cosmos-sdk/client/lcd/statik"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why is this being imported if it's not used ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import will trgger side effects: call the init function of the package. We can also call the init function explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it explicitly, makes the code more obvious

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that I can't do that.

func init() {
}

The init function is private, so I can't call it explicitly from outside. Besides, this file is generated by statik, so we should avoid to edit it maunally.

* Regenerate statik.go file
```
statik -src=client/lcd/swaggerui -dest=client/lcd
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens after the new statik.go file is generated ? Just run gaiacli lite-server and it's done ? Maybe add that section here as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(My question is mainly how do I see the generated docs afterwards)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can start lite-server with this command:

gaiacli lite-server --chain-id=<chain_id>

Then visit the following url with your explorer:

http://localhost:1317/swaggerui/


## Steps

* Install the command line tool first.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace * for numbers (e.g 1. , 2. , ... ) since they are steps

Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, I left a couple of comments. Also please make sure to update PENDING.md since this are breaking changes (specially endpoint renaming).

Also please update Gaia-lite section of the docs with a link to how to update the API (in this case it would be something like https://cosmos.network/docs/sdk/client/lcd/README.html). I remember you already made some comments in that section in one of your other PRs, maybe merge both of them within the Gaia-lite section ?

@fedekunze
Copy link
Collaborator

@HaoyangLiu client/lcd/README.md is not within the docs folder, can you add them there as well ?

@fedekunze
Copy link
Collaborator

also Golint is failing in CircleCI, make sure that's fixed as well. Thanks !

@codecov
Copy link

codecov bot commented Sep 2, 2018

Codecov Report

Merging #2215 into develop will decrease coverage by 0.44%.
The diff coverage is 4%.

@@            Coverage Diff             @@
##           develop   #2215      +/-   ##
==========================================
- Coverage    61.15%   60.7%   -0.45%     
==========================================
  Files          123     125       +2     
  Lines         7391    7569     +178     
==========================================
+ Hits          4520    4595      +75     
- Misses        2558    2660     +102     
- Partials       313     314       +1

@HaoyangLiu HaoyangLiu requested a review from zramsay as a code owner September 2, 2018 14:30
// add new key REST handler
func paramCheck(m NewKeyBody, kb keys.Keybase) (int, string) {
if m.Name == "" {
return http.StatusBadRequest, fmt.Sprintf("You have to specify a name for the locally stored account.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for fmt.Sprintf here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to keep the code style consistent. Removing it looks better.

return http.StatusBadRequest, fmt.Sprintf("You have to specify a name for the locally stored account.")
}
if m.Password == "" {
return http.StatusBadRequest, fmt.Sprintf("You have to specify a password for the locally stored account.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@HaoyangLiu
Copy link
Author

HaoyangLiu commented Sep 3, 2018

@jackzampolin @cwgoes
Here is prototype of gaia-lite. I have some questions:

  1. If gaia-lite is done. Do we need to keep subcommand rest-server.
  2. I noticed that almost all APIs of gaia-lite are also included in rest-server. So how about merge them? Here I just renamed rest-server to lite-server and added swagger-ui.
  3. Now the API docs locates in client/lcd/swaggerui/swagger.json. Based on this json file, statik can generate client/lcd/statik/statik.go. So once client/lcd/statik/statik.go is generated, the supported modules and swagger host IP are solidified. This will result in some problems:
    1. We can't implement modular APIs
    2. If gaia-lite is running on remote server or --laddr option is assigned to other value, the swagger-ui will not work well. Because swagger-ui only send http request to localhost:1317

@fedekunze
Do you have some suggestion about the above two problems?

…ing in indent to make response more readable
@zramsay
Copy link
Contributor

zramsay commented Oct 2, 2018

if there's a simple build process to generate to bunch of files required to serve the RPC docs, the website Makefile can handle this (like it does for the regular docs, it's basically a cp then the build commands). would be good to have some instructions about building & serving the RPC docs added to the docs/DOCS_READM.md

Copy link
Contributor

@zramsay zramsay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requires instructions for building/serving the RPC docs added to docs/DOCS_README.md

@@ -0,0 +1,18 @@
# How to update API docs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this whole file to a section in docs/DOCS_README.md


1. Install the swagger-ui generate tool first.
```
make get_tools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which Makefile is this meant to target? should specify that this command is to be run from ... ? (root of repo i think)

@HaoyangLiu
Copy link
Author

@zramsay
The API docs are generated by during building gaiad and gaiacli. When you execute make install or make build, the make target update_gaia_lite_docs will be executed too. It will generate a golang file : client/lcd/statik/statik.go which contains the API docs. So the docs building process is transient.
Besides, I have move the update_API_docs.md to docs/DOCS_README.md

@HaoyangLiu
Copy link
Author

@cwgoes
The JS files are static and they won't be changed later. As for the map files, do you think I should unminify them? I'm sorry I failed to get your point here.

@cwgoes
Copy link
Contributor

cwgoes commented Oct 2, 2018

@HaoyangLiu The JS files are OK, it's not a big deal. Only remaining issue is investigating why localnet CI is failing - looks like something in the static file handling package?

@HaoyangLiu
Copy link
Author

HaoyangLiu commented Oct 2, 2018

@cwgoes
I have investigated the issue, please refer to #2331
The docker image circleci/golang:1.10.3 actually has golang1.9 installed. However, the swagger-ui tool statik requires golang version large than 1.10.

@HaoyangLiu
Copy link
Author

@cwgoes
Due to some change in gaiacli config, my previous change in creating certifier doesn't work well. For getting status command, viper.IsSet(client.FlagTrustNode) also returns true though client.FlagTrustNode is not predefined.
So I removed the change and add another change in get status

@cwgoes
Copy link
Contributor

cwgoes commented Oct 3, 2018

Due to some change in gaiacli config, my previous change in creating certifier doesn't work well. For getting status command, viper.IsSet(client.FlagTrustNode) also returns true though client.FlagTrustNode is not predefined.
So I removed the change and add another change in get status

We've temporarily disabled light client verification in the LCD tests due to an upstream Tendermint issue, so I think you don't need to also do it in getStatus.

This needs to be rebased against develop since the TM 0.24 PR was merged, then we should be good to merge it.

@HaoyangLiu
Copy link
Author

@cwgoes
Currently the gaiacli status command can't be executed. Please check my execution log:

lhy@lhy-ubuntu:~$ gaiacli status
Must specify these options: --chain-id  when --trust-node is false
lhy@lhy-ubuntu:~$ gaiacli status --chain-id test-chain-dNvAWi
ERROR: unknown flag: --chain-id

Now when executing get status, it will create certifier which require --chain-id. However, certifier should not be created in this command. So I added viper.Set(client.FlagTrustNode, true):

func printNodeStatus(cmd *cobra.Command, args []string) error {
	// No need to verify proof in getting node status
	viper.Set(client.FlagTrustNode, true)
	cliCtx := context.NewCLIContext()
	status, err := getNodeStatus(cliCtx)
	if err != nil {
		return err
	}

@jackzampolin
Copy link
Member

Excited to see this merged!

@cwgoes
Copy link
Contributor

cwgoes commented Oct 4, 2018

Now when executing get status, it will create certifier which require --chain-id. However, certifier should not be created in this command. So I added viper.Set(client.FlagTrustNode, true)

I see, OK - instead, let's use cliContext.WithTrustNode, viper.Set is a bit of an anti-pattern (hard to track global variable changes).

I made a PR - https://github.com/HaoyangLiu/cosmos-sdk/pull/8.

@HaoyangLiu
Copy link
Author

@cwgoes
the golang version of docker image circleci/golang:1.11.1 is 1.9 too which is very strange.

go version go1.9.1 linux/amd64

Please check the CI failure: https://circleci.com/gh/cosmos/cosmos-sdk/34860#config/containers/0

@cwgoes
Copy link
Contributor

cwgoes commented Oct 4, 2018

the golang version of docker image circleci/golang:1.11.1 is 1.9 too which is very strange.

That's strange. I'll try another image in a separate PR (easier to test with PRs against this repo).

@cwgoes cwgoes merged commit 9f67e8a into cosmos:develop Oct 4, 2018
@@ -1,5 +1,6 @@
package utils

import "C"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have broken cross compilation between Darwin and Linux. Can we safely remove this line or is there any reason why we shouldn't do so?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want this cross-compilation ability can we test for it in CI? Then we'll know in the future if we break it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd second the idea, though we have to figure out how go about it - golang/go#19470

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alessio pushed a commit that referenced this pull request Oct 17, 2018
It was introduced in #2215, it's presently unnecessary and
it is clearly breaking cross compilation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: API Breaking Breaking changes that impact APIs and the SDK only (not state machine).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants