From c1c6b77d1a721d9bb9b2579be13bf044306604b5 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 18 Aug 2022 12:19:27 +0200 Subject: [PATCH] refactor: helper method for register swagger api (#12955) * refactor: helper method for register swagger api * updates swagger.yaml * `make update-swagger-docs` --- app.go | 21 ++------------------- app_legacy.go | 21 ++------------------- 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/app.go b/app.go index 3196951407c..9854d15749f 100644 --- a/app.go +++ b/app.go @@ -5,12 +5,9 @@ package simapp import ( _ "embed" "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" @@ -22,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/api" "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -88,9 +86,6 @@ 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" ) var ( @@ -380,22 +375,10 @@ func (app *SimApp) SimulationManager() *module.SimulationManager { // API server. func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) { app.App.RegisterAPIRoutes(apiSvr, apiConfig) - // register swagger API from root so that other applications can override easily - if apiConfig.Swagger { - RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router) - } -} - -// RegisterSwaggerAPI registers swagger route with API Server -func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router) { - statikFS, err := fs.New() - if err != nil { + if err := server.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger); 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 diff --git a/app_legacy.go b/app_legacy.go index 7cfd3df5bed..641dcd6641d 100644 --- a/app_legacy.go +++ b/app_legacy.go @@ -5,12 +5,9 @@ package simapp import ( "encoding/json" "io" - "net/http" "os" "path/filepath" - "github.com/gorilla/mux" - "github.com/rakyll/statik/fs" "github.com/spf13/cast" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" @@ -98,9 +95,6 @@ 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" @@ -631,8 +625,8 @@ func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICon ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter) // register swagger API from root so that other applications can override easily - if apiConfig.Swagger { - RegisterSwaggerAPI(clientCtx, apiSvr.Router) + if err := server.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger); err != nil { + panic(err) } } @@ -651,17 +645,6 @@ func (app *SimApp) RegisterTendermintService(clientCtx client.Context) { ) } -// RegisterSwaggerAPI registers swagger route with API Server -func RegisterSwaggerAPI(_ 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 func GetMaccPerms() map[string][]string { dupMaccPerms := make(map[string][]string)