Skip to content

Commit

Permalink
Merge pull request #987 from EmpowerPlastic/swagger-setup
Browse files Browse the repository at this point in the history
Swagger setup
  • Loading branch information
gjermundgaraba authored Jul 9, 2023
2 parents 6dabb90 + 020e3d8 commit 0b1de11
Show file tree
Hide file tree
Showing 13 changed files with 64,569 additions and 8 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/chain_docs-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Update Swagger docs"

on:
workflow_dispatch:
push:
branches:
- main
paths:
- 'chain/proto/**' # On updating proto

jobs:
build:
name: Update Swagger docs
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: earthly/actions/setup-earthly@v1
with:
version: v0.6.22

- run: earthly --ci --output +swagger-docs
working-directory: .chain/

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: Update chain Swagger docs
title: Update chain Swagger docs
branch: update-swagger-docs

10 changes: 10 additions & 0 deletions chain/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,21 @@ swagger-gen:
chmod +x /usr/local/bin/swagger
# Install protoc-gen-swagger
RUN go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@latest
# Install statik
RUN go install github.com/rakyll/statik@latest
RUN make swagger-gen

swagger-md:
FROM +swagger-gen
RUN cd client/docs/swagger-ui && swagger generate markdown
SAVE ARTIFACT client/docs/swagger-ui/swagger.yaml
SAVE ARTIFACT client/docs/swagger-ui/markdown.md

swagger-docs:
FROM +swagger-gen
SAVE ARTIFACT client/docs/* AS LOCAL client/docs/


docgen:
FROM +build-prep
RUN cd ./scripts/docgen && go run ./...
Expand Down
14 changes: 12 additions & 2 deletions chain/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ test-sim-nondeterminism:
### Proto ###
###############################################################################

protoVer=0.11.5
protoVer=0.11.6
protoImageName=ghcr.io/cosmos/proto-builder:$(protoVer)
protoImage=$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace $(protoImageName)

Expand All @@ -222,13 +222,23 @@ proto:

swagger-gen:
@echo
@echo "=========== Generate Message ============"
@echo "=========== Generate Swagger Docs ============"
@echo
./scripts/generate-docs.sh
$(MAKE) update-swagger-docs
@echo
@echo "=========== Generate Complete ============"
@echo

update-swagger-docs:
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;\
else \
echo "\033[92mSwagger docs are in sync\033[0m";\
fi

###############################################################################
### Serve ###
###############################################################################
Expand Down
20 changes: 20 additions & 0 deletions chain/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"

Expand Down Expand Up @@ -119,6 +120,8 @@ import (
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"

"github.com/EmpowerPlastic/empowerchain/app/upgrades"
Expand All @@ -131,6 +134,9 @@ import (
proofofexistencemoduletypes "github.com/EmpowerPlastic/empowerchain/x/proofofexistence"
proofofexistencemodulekeeper "github.com/EmpowerPlastic/empowerchain/x/proofofexistence/keeper"
proofofexistencemodule "github.com/EmpowerPlastic/empowerchain/x/proofofexistence/module"

// unnamed import of statik for swagger UI support
_ "github.com/EmpowerPlastic/empowerchain/client/docs/statik"
)

const (
Expand Down Expand Up @@ -938,6 +944,10 @@ func (app *EmpowerApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.AP
nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

if apiConfig.Swagger {
RegisterSwaggerAPI(clientCtx, apiSvr.Router)
}
}

func (app *EmpowerApp) setupUpgradeStoreLoaders() {
Expand Down Expand Up @@ -1039,3 +1049,13 @@ func BlockedAddresses() map[string]bool {

return modAccAddrs
}

// 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))
}
145 changes: 145 additions & 0 deletions chain/client/docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,151 @@
"Params": "ProofOfExistenceParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/auth/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "AuthParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/bank/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "BankParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/base/tendermint/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "BaseParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/distribution/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "DistributionParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/evidence/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "EvidenceParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/gov/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "GovParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/gov/v1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "GovV1Params",
"Proposal": "GovV1Proposal",
"Proposals": "GovV1Proposal",
"Vote": "GovV1Vote",
"Votes": "GovV1Votes",
"Deposit": "GovV1Deposit",
"Deposits": "GovV1Deposit",
"TallyResult": "GovV1TallyResult"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/mint/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "MintParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/params/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "Params"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/slashing/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "SlashingParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/staking/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "StakingParams",
"DelegatorValidators": "StakingDelegatorValidators"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/tx/v1beta1/service.swagger.json",
"dereference": {
"circular": "ignore"
}
},
{
"url": "./tmp-swagger-gen/cosmos/upgrade/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "UpgradeParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/authz/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "AuthzParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/feegrant/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "FeegrantParams"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/nft/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "NftParams",
"Balance": "NftBalance"
}
}
},
{
"url": "./tmp-swagger-gen/cosmos/group/v1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "GroupParams",
"Proposal": "GroupProposal",
"TallyResult": "GroupTallyResult"
}
}
}
]
}
6 changes: 6 additions & 0 deletions chain/client/docs/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package docs

import "embed"

//go:embed swagger-ui
var SwaggerUI embed.FS
3 changes: 3 additions & 0 deletions chain/client/docs/statik/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package statik

// This just for fixing the error in importing empty github.com/cosmos/cosmos-sdk/client/docs/statik
14 changes: 14 additions & 0 deletions chain/client/docs/statik/statik.go

Large diffs are not rendered by default.

Loading

0 comments on commit 0b1de11

Please sign in to comment.