Skip to content

Commit

Permalink
refactor: remove pkg/openapiconsole import in scaffolded chain (#3337)
Browse files Browse the repository at this point in the history
* fix typo

* add nodeservice

* add nodeservice

* add doc

* remove need for openAPI import

* no more import

* no more import

* changelog

* gofmt on go.plush files

* update changelog

* refactor

Co-authored-by: Thomas Bruyelle <[email protected]>
  • Loading branch information
Alex Johnson and tbruyelle authored Dec 26, 2022
1 parent d638a68 commit e2cc76c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
- [#3183](https://github.com/ignite/cli/pull/3183/) Make config optional for init phase.
- [#3224](https://github.com/ignite/cli/pull/3224) Remove grpc_* prefix from query files in scaffolded chains
- [#3229](https://github.com/ignite/cli/pull/3229) Rename `campaign` to `project` in ignite network set of commands
- [#3244](https://github.com/ignite/cli/pull/3244) updated actions.yml for resolving deprecation message
- [#3244](https://github.com/ignite/cli/pull/3244) Update actions.yml for resolving deprecation message
- [#3337](https://github.com/ignite/cli/pull/3337) Remove `pkg/openapiconsole` import from scaffold template.
- [#3337](https://github.com/ignite/cli/pull/3337) Register`nodeservice` grpc in `app.go` template.

### Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/cosmosgen/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func generateOpenAPISpec(g *generator) error {
return nil
}

// protoc openapi generator acts weird on conccurrent run, so do not use goroutines here.
// protoc openapi generator acts weird on concurrent run, so do not use goroutines here.
if err := add(g.appPath, g.appModules); err != nil {
return err
}
Expand Down
41 changes: 23 additions & 18 deletions ignite/templates/app/files/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package app
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -102,7 +102,6 @@ import (
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"
"github.com/ignite/cli/ignite/pkg/openapiconsole"

// this line is used by starport scaffolding # stargate/app/moduleImport

Expand Down Expand Up @@ -492,7 +491,7 @@ func New(

// this line is used by starport scaffolding # stargate/app/keeperDefinition

/**** IBC Routing ****/
/**** IBC Routing ****/

// Sealing prevents other modules from creating scoped sub-keepers
app.CapabilityKeeper.Seal()
Expand All @@ -506,21 +505,21 @@ func New(

/**** Module Hooks ****/

// register hooks after all modules have been initialized
// register hooks after all modules have been initialized

app.StakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
// insert staking hooks receivers here
app.DistrKeeper.Hooks(),
app.SlashingKeeper.Hooks(),
),
)
app.StakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
// insert staking hooks receivers here
app.DistrKeeper.Hooks(),
app.SlashingKeeper.Hooks(),
),
)

app.GovKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// insert governance hooks receivers here
),
)
app.GovKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// insert governance hooks receivers here
),
)

/**** Module Options ****/

Expand Down Expand Up @@ -815,13 +814,14 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig
authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register new tendermint queries routes from grpc-gateway.
tmservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register node gRPC service for grpc-gateway.
nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// Register grpc-gateway routes for all modules.
ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

// register app's OpenAPI routes.
apiSvr.Router.Handle("/static/openapi.yml", http.FileServer(http.FS(docs.Docs)))
apiSvr.Router.HandleFunc("/", openapiconsole.Handler(Name, "/static/openapi.yml"))
docs.RegisterOpenAPIService(Name, apiSvr.Router)
}

// RegisterTxService implements the Application.RegisterTxService method.
Expand All @@ -839,6 +839,11 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
)
}

// RegisterNodeService implements the Application.RegisterNodeService method.
func (app *App) RegisterNodeService(clientCtx client.Context) {
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter())
}

// GetMaccPerms returns a copy of the module account permissions
func GetMaccPerms() map[string][]string {
dupMaccPerms := make(map[string][]string)
Expand Down
39 changes: 37 additions & 2 deletions ignite/templates/app/files/docs/docs.go.plush
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
package docs

import "embed"
import (
"embed"
httptemplate "html/template"
"net/http"

"github.com/gorilla/mux"
)

const (
apiFile = "/static/openapi.yml"
indexFile = "template/index.tpl"
)


//go:embed static
var Docs embed.FS
var Static embed.FS

//go:embed template
var template embed.FS

func RegisterOpenAPIService(appName string, rtr *mux.Router) {
rtr.Handle(apiFile, http.FileServer(http.FS(Static)))
rtr.HandleFunc("/", handler(appName))
}

// handler returns an http handler that servers OpenAPI console for an OpenAPI spec at specURL.
func handler(title string) http.HandlerFunc {
t, _ := httptemplate.ParseFS(template, indexFile)

return func(w http.ResponseWriter, req *http.Request) {
t.Execute(w, struct {
Title string
URL string
}{
title,
apiFile,
})
}
}
28 changes: 28 additions & 0 deletions ignite/templates/app/files/docs/template/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="//unpkg.com/[email protected]/swagger-ui.css" />
<link rel="icon" type="image/png" href="//unpkg.com/[email protected]/favicon-16x16.png" />
</head>
<body>
<div id="swagger-ui"></div>

<script src="//unpkg.com/[email protected]/swagger-ui-bundle.js"></script>
<script>
// init Swagger for faucet's openapi.yml.
window.onload = function() {
window.ui = SwaggerUIBundle({
url: {{ .URL }},
dom_id: "#swagger-ui",
deepLinking: true,
layout: "BaseLayout",
});
}
</script>
</body>
</html>
Footer
© 2022 GitHub, Inc.
Footer navigation

0 comments on commit e2cc76c

Please sign in to comment.