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

refactor: helper method for register swagger api #12352

21 changes: 21 additions & 0 deletions baseapp/register_swagger_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package baseapp

import (
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
)

// RegisterSwaggerAPI - a common function which registers swagger route with API Server
func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router, swaggerEnabled bool) {
if swaggerEnabled {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
rtr.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
}
}
21 changes: 2 additions & 19 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package simapp
import (
_ "embed"
"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"
Expand All @@ -20,7 +17,6 @@ import (
"github.com/cosmos/cosmos-sdk/depinject"
deepto98 marked this conversation as resolved.
Show resolved Hide resolved

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -411,21 +407,8 @@ func (app *SimApp) SimulationManager() *module.SimulationManager {
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 {
panic(err)
}

staticServer := http.FileServer(statikFS)
rtr.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
// register swagger API (using common function from baseapp package) from root so that other applications can override easily
baseapp.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger)
}

// GetMaccPerms returns a copy of the module account permissions
Expand Down