Register swagger API (#7246)

* init

* Fix statik gen

* Fix swagger

* Change swagger url

* Fix swagger serve

* remove ibc swagger from legacy docs

* Add old routes config

* Move swagger api to app.go

* add godoc

* Fix inputs

* Fix swagger dir

* Fix statik

* refactor

* fmt

* fix doc

* Fix swagger config check
This commit is contained in:
Anil Kumar Kammari 2020-09-19 06:04:56 +05:30 committed by GitHub
parent d6357e77b4
commit ca7b31dd5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 29607 additions and 15663 deletions

View File

@ -170,7 +170,7 @@ go.sum: go.mod
###############################################################################
update-swagger-docs: statik
$(BINDIR)/statik -src=client/grpc-gateway -dest=client/grpc-gateway -f -m
$(BINDIR)/statik -src=client/docs/swagger-ui -dest=client/docs -f -m
@if [ -n "$(git status --porcelain)" ]; then \
echo "\033[91mSwagger docs are out of sync!!!\033[0m";\
exit 1;\

View File

@ -1,10 +1,17 @@
{
"swagger": "2.0",
"info": {
"title": "Cosmos SDK - GRPC Gateway",
"title": "Cosmos SDK - Legacy REST and gRPC Gateway docs",
"description": "A REST interface for state queries, legacy transactions",
"version": "1.0.0"
},
"apis": [
{
"url": "./client/docs/swagger_legacy.yaml",
"dereference": {
"circular": "ignore"
}
},
{
"url": "./cosmos/auth/v1beta1/query.swagger.json",
"operationIds": {

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,8 @@ done
# combine swagger files
# uses nodejs package `swagger-combine`.
# all the individual swagger files need to be configured in `config.json` for merging
swagger-combine ./client/grpc-gateway/config.json -o ./client/grpc-gateway/swagger.json --continueOnConflictingPaths true --includeDefinitions true
swagger-combine ./client/docs/config.json -o ./client/docs/swagger-ui/swagger.yaml -f yaml --continueOnConflictingPaths true --includeDefinitions true
# clean swagger files
find ./ -name 'query.swagger.json' -exec rm {} \;
rm -rf cosmos
rm -rf ibc

View File

@ -11,7 +11,6 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/rakyll/statik/fs"
"github.com/tendermint/tendermint/libs/log"
tmrpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
@ -83,10 +82,6 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
// and are delegated to the Tendermint JSON RPC server. The process is
// non-blocking, so an external signal handler must be used.
func (s *Server) Start(cfg config.Config) error {
if cfg.API.Swagger {
s.registerSwaggerUI()
}
if cfg.Telemetry.Enabled {
m, err := telemetry.New(cfg.Telemetry)
if err != nil {
@ -109,6 +104,7 @@ func (s *Server) Start(cfg config.Config) error {
}
s.registerGRPCRoutes()
s.listener = listener
var h http.Handler = s.Router
@ -126,16 +122,6 @@ func (s *Server) Close() error {
return s.listener.Close()
}
func (s *Server) registerSwaggerUI() {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
s.Router.PathPrefix("/legacy").Handler(staticServer)
}
func (s *Server) registerGRPCRoutes() {
s.Router.PathPrefix("/").Handler(s.GRPCRouter)
}

View File

@ -227,6 +227,7 @@ func GetConfig(v *viper.Viper) Config {
},
API: APIConfig{
Enable: v.GetBool("api.enable"),
Swagger: v.GetBool("api.swagger"),
Address: v.GetString("api.address"),
MaxOpenConnections: v.GetUint("api.max-open-connections"),
RPCReadTimeout: v.GetUint("api.rpc-read-timeout"),

View File

@ -246,7 +246,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
WithClient(local.New(tmNode))
apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"))
app.RegisterAPIRoutes(apiSrv)
app.RegisterAPIRoutes(apiSrv, config.API)
errCh := make(chan error)
go func() {

View File

@ -11,6 +11,7 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
)
type (
@ -31,7 +32,7 @@ type (
Application interface {
abci.Application
RegisterAPIRoutes(*api.Server)
RegisterAPIRoutes(*api.Server, config.APIConfig)
// RegisterGRPCServer registers gRPC services directly with the gRPC
// server.

View File

@ -2,9 +2,12 @@ package simapp
import (
"io"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
@ -12,10 +15,12 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
@ -77,6 +82,9 @@ import (
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
)
const appName = "SimApp"
@ -530,12 +538,29 @@ func (app *SimApp) SimulationManager() *module.SimulationManager {
// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server) {
func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
clientCtx := apiSvr.ClientCtx
rpc.RegisterRoutes(clientCtx, apiSvr.Router)
authrest.RegisterTxRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterRESTRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterGRPCRoutes(apiSvr.ClientCtx, apiSvr.GRPCRouter)
// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
RegisterSwaggerAPI(clientCtx, apiSvr.Router)
}
}
// RegisterSwaggerAPI registers swagger route with API Server
func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
rtr.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
}
// GetMaccPerms returns a copy of the module account permissions

View File

@ -64,7 +64,7 @@ func startInProcess(cfg Config, val *Validator) error {
WithClient(val.RPCClient)
apiSrv := api.New(val.ClientCtx, logger.With("module", "api-server"))
app.RegisterAPIRoutes(apiSrv)
app.RegisterAPIRoutes(apiSrv, val.AppConfig.API)
errCh := make(chan error)