Skip to content

Commit

Permalink
feat: use go embed for swagger (#1175)
Browse files Browse the repository at this point in the history
* replace `statik` with built-in `embed`
  • Loading branch information
tkxkd0159 authored Nov 8, 2023
1 parent d232bd6 commit 93fa67d
Show file tree
Hide file tree
Showing 19 changed files with 18 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .github/actions/generate-swagger/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ runs:
steps:
- run: make proto-swagger-gen
shell: sh
- run: make update-swagger-docs
shell: sh
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (crypto) [\#1163](https://github.com/Finschia/finschia-sdk/pull/1163) Update some secp256k1 logics with latest `dcrec`
* (x/crisis) [#1167](https://github.com/Finschia/finschia-sdk/pull/1167) Use `CacheContext()` in `AssertInvariants()`
* (chore) [\#1168](https://github.com/Finschia/finschia-sdk/pull/1168) Replace `ExactArgs(0)` with `NoArgs()` in `x/upgrade` module
* (server) [\#1175](https://github.com/Finschia/finschia-sdk/pull/1175) Use go embed for swagger

### Bug Fixes
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
Expand Down
12 changes: 1 addition & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ go.sum: go.mod
### Documentation ###
###############################################################################

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

godocs:
@echo "--> Wait a few seconds and visit http://localhost:6060/pkg/github.com/Finschia/finschia-sdk/types"
godoc -http=:6060
Expand Down Expand Up @@ -357,7 +347,7 @@ lint-fix: golangci-lint
.PHONY: lint lint-fix golangci-lint

format: golangci-lint
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/docs/statik/statik.go" -not -path "./tests/mocks/*" -not -name "*.pb.go" -not -name "*.pb.gw.go" -not -name "*.pulsar.go" -not -path "./crypto/keys/secp256k1/*" | xargs gofumpt -w -l
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./tests/mocks/*" -not -name "*.pb.go" -not -name "*.pb.gw.go" -not -name "*.pulsar.go" -not -path "./crypto/keys/secp256k1/*" | xargs gofumpt -w -l
golangci-lint run --fix
.PHONY: format

Expand Down
6 changes: 6 additions & 0 deletions 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: 0 additions & 3 deletions client/docs/statik/init.go

This file was deleted.

13 changes: 0 additions & 13 deletions client/docs/statik/statik.go

This file was deleted.

1 change: 0 additions & 1 deletion client/docs/swagger-ui/swagger-ui-bundle.js.map

This file was deleted.

1 change: 0 additions & 1 deletion client/docs/swagger-ui/swagger-ui-es-bundle-core.js.map

This file was deleted.

1 change: 0 additions & 1 deletion client/docs/swagger-ui/swagger-ui-es-bundle.js.map

This file was deleted.

1 change: 0 additions & 1 deletion client/docs/swagger-ui/swagger-ui-standalone-preset.js.map

This file was deleted.

1 change: 0 additions & 1 deletion client/docs/swagger-ui/swagger-ui.js.map

This file was deleted.

1 change: 0 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ ignore:
- "x/**/test_common.go"
- "*_cmd.go"
- "contrib"
- "statik.go"
- "root.go"
- "x/**/module.go"
- "x/**/errors.go"
Expand Down
13 changes: 3 additions & 10 deletions contrib/devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,23 @@ GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com
BUF_VERSION ?= 0.11.0

TOOLS_DESTDIR ?= $(GOPATH)/bin
STATIK = $(TOOLS_DESTDIR)/statik
RUNSIM = $(TOOLS_DESTDIR)/runsim

tools: tools-stamp
tools-stamp: statik runsim
tools-stamp: runsim
# Create dummy file to satisfy dependency and avoid
# rebuilding when this Makefile target is hit twice
# in a row.
touch $@

# Install the runsim binary
statik: $(STATIK)
$(STATIK):
@echo "Installing statik..."
@go install github.com/rakyll/[email protected]

# Install the runsim binary
runsim: $(RUNSIM)
$(RUNSIM):
@echo "Installing runsim..."
@go install github.com/cosmos/tools/cmd/[email protected]

tools-clean:
rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM)
rm -f $(GOLANGCI_LINT) $(RUNSIM)
rm -f tools-stamp

.PHONY: tools-clean statik runsim
.PHONY: tools-clean runsim
2 changes: 1 addition & 1 deletion contrib/githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ f_check_cmds
if [[ $STAGED_GO_FILES != "" ]]; then
f_echo_stderr "[pre-commit] fmt'ing staged files..."
for file in $STAGED_GO_FILES; do
if [[ $file =~ vendor/ ]] || [[ $file =~ client/docs/statik/ ]] || [[ $file =~ tests/mocks/ ]] || [[ $file =~ \.pb\.go ]]; then
if [[ $file =~ vendor/ ]] || [[ $file =~ tests/mocks/ ]] || [[ $file =~ \.pb\.go ]]; then
continue
fi

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/common v0.45.0
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.1
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.8.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,6 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
2 changes: 0 additions & 2 deletions scripts/update-swagger-ui-statik.sh

This file was deleted.

2 changes: 0 additions & 2 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
ostrpcserver "github.com/Finschia/ostracon/rpc/jsonrpc/server"

"github.com/Finschia/finschia-sdk/client"
// unnamed import of statik for swagger UI support
_ "github.com/Finschia/finschia-sdk/client/docs/statik"
"github.com/Finschia/finschia-sdk/codec/legacy"
"github.com/Finschia/finschia-sdk/server/config"
"github.com/Finschia/finschia-sdk/telemetry"
Expand Down
13 changes: 6 additions & 7 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package simapp
import (
"encoding/json"
"io"
"io/fs"
"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"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand All @@ -20,8 +20,7 @@ import (

"github.com/Finschia/finschia-sdk/baseapp"
"github.com/Finschia/finschia-sdk/client"
// unnamed import of statik for swagger UI support
_ "github.com/Finschia/finschia-sdk/client/docs/statik"
"github.com/Finschia/finschia-sdk/client/docs"
nodeservice "github.com/Finschia/finschia-sdk/client/grpc/node"
"github.com/Finschia/finschia-sdk/client/grpc/ocservice"
"github.com/Finschia/finschia-sdk/client/grpc/tmservice"
Expand Down Expand Up @@ -662,7 +661,7 @@ func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICon

// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
RegisterSwaggerAPI(clientCtx, apiSvr.Router)
RegisterSwaggerAPI(apiSvr.Router)
}
}

Expand All @@ -683,13 +682,13 @@ func (app *SimApp) RegisterNodeService(clientCtx client.Context) {
}

// RegisterSwaggerAPI registers swagger route with API Server
func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) {
statikFS, err := fs.New()
func RegisterSwaggerAPI(rtr *mux.Router) {
root, err := fs.Sub(docs.SwaggerUI, "swagger-ui")
if err != nil {
panic(err)
}

staticServer := http.FileServer(statikFS)
staticServer := http.FileServer(http.FS(root))
rtr.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
}

Expand Down

0 comments on commit 93fa67d

Please sign in to comment.