Skip to content

Commit

Permalink
Implement swagger docs and fix path validation (#434)
Browse files Browse the repository at this point in the history
* Add swagger setup

* Add some routes docs and swagger ui

* Add few more route docs

* Add swagger docs for remaining routes

* Fix golint issues

* Fix unused lint issues

* check chain-id in AddChain
  • Loading branch information
akhilkumarpilli committed Mar 3, 2021
1 parent d007627 commit 3eb0394
Show file tree
Hide file tree
Showing 13 changed files with 2,939 additions and 42 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ build-wasmd:
delete-chains:
@echo "Removing the ./chain-code/ directory..."
@rm -rf ./chain-code

check-swagger:
which swagger || (GO111MODULE=off go get -u github.com/go-swagger/go-swagger/cmd/swagger)

update-swagger-docs: check-swagger
swagger generate spec -o ./docs/swagger-ui/swagger.yaml

6 changes: 5 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,15 @@ func getAPICmd() *cobra.Command {
// Data for this should be stored in the ServicesManager struct
r.HandleFunc("/listen/{path}/{strategy}/{name}", PostRelayerListenHandler(sm)).Methods("POST")

fs := http.FileServer(http.Dir("./docs/swagger-ui"))
r.PathPrefix("/").Handler(fs)

fmt.Println("listening on", config.Global.APIListenPort)

if err := http.ListenAndServe(config.Global.APIListenPort, r); err != nil {
return err
}

fmt.Println("listening on", config.Global.APIListenPort)
return nil
},
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,10 @@ type addChainRequest struct {
GasAdjustment string `json:"gas-adjustment"`
GasPrices string `json:"gas-prices"`
TrustingPeriod string `json:"trusting-period"`
FilePath string `json:"file"`
URL string `json:"url"`
// required: false
FilePath string `json:"file"`
// required: false
URL string `json:"url"`
}

// PostChainHandler handles the route
Expand Down Expand Up @@ -678,6 +680,11 @@ func PutChainHandler(w http.ResponseWriter, r *http.Request) {
// DeleteChainHandler handles the route
func DeleteChainHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := config.Chains.Get(vars["name"])
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
if err := overWriteConfig(config.DeleteChain(vars["name"])); err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
Expand Down
12 changes: 8 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ func newDefaultGlobalConfig() GlobalConfig {

// AddChain adds an additional chain to the config
func (c *Config) AddChain(chain *relayer.Chain) (err error) {
if chain.ChainID == "" {
return fmt.Errorf("chain ID cannot be empty")
}
chn, err := c.Chains.Get(chain.ChainID)
if chn == nil || err == nil {
return fmt.Errorf("chain with ID %s already exists in config", chain.ChainID)
Expand Down Expand Up @@ -564,15 +567,16 @@ func (c *Config) ValidatePathEnd(pe *relayer.PathEnd) error {
return err
}

chain, err := c.Chains.Get(pe.ChainID)
if err != nil {
return err
}

// if the identifiers are empty, don't do any validation
if pe.ClientID == "" && pe.ConnectionID == "" && pe.ChannelID == "" {
return nil
}

chain, err := c.Chains.Get(pe.ChainID)
if err != nil {
return err
}
// NOTE: this is just to do validation, the path
// is not written to the config file
if err = chain.SetPath(pe); err != nil {
Expand Down
Loading

0 comments on commit 3eb0394

Please sign in to comment.