Skip to content

Commit

Permalink
add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
tubackkhoa committed Jan 6, 2023
1 parent db3075b commit 6a3e980
Show file tree
Hide file tree
Showing 14 changed files with 117,881 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ data/validator*/U*
data/validator*/config/addrbook.json

upgrade-tests/data*
upgrade-tests/exported-genesis-new.json
upgrade-tests/exported-genesis-new.json

tmp
3 changes: 3 additions & 0 deletions module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ lint:
### Protobuf ###
###############################################################################

proto-swagger:
./contrib/local/protocgen-swagger.sh $(SRC_DIR)

proto-gen:
./contrib/local/protocgen.sh

Expand Down
3 changes: 2 additions & 1 deletion module/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ import (
bech32ibctypes "github.com/osmosis-labs/bech32-ibc/x/bech32ibc/types"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
// _ "github.com/cosmos/cosmos-sdk/client/docs/statik"
_ "github.com/Gravity-Bridge/Gravity-Bridge/module/doc/statik"

// Tharsis Ethermint
ethante "github.com/evmos/ethermint/app/ante"
Expand Down
80 changes: 80 additions & 0 deletions module/contrib/local/protocgen-swagger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

set -eo pipefail

# go get ./...
# go install github.com/cosmos/gogoproto/protoc-gen-gocosmos
# go install github.com/grpc-ecosystem/grpc-gateway/[email protected]
# apk add nodejs-current npm
# npm install -g swagger-combine

BASEDIR=$(dirname $0)
PROJECTDIR=$BASEDIR/../..
# default is tmp folder
SOURCEDIR=$(realpath ${1:-$PROJECTDIR/tmp})
DOC_DIR=$(realpath $PROJECTDIR/doc)

COSMOS_SDK_DIR=${COSMOS_SDK_DIR:-$(go list -f "{{ .Dir }}" -m github.com/cosmos/cosmos-sdk)}
IBC_DIR=${IBC_DIR:-$(go list -f "{{ .Dir }}" -m github.com/cosmos/ibc-go/v3)}


# scan all folders that contain proto file
proto_dirs=$(find $PROJECTDIR/proto $COSMOS_SDK_DIR/proto $COSMOS_SDK_DIR/third_party/proto $IBC_DIR/proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)

GEN_DIR=$SOURCEDIR/swagger-gen
# clean swagger files
rm -rf $GEN_DIR
mkdir -p $GEN_DIR

for dir in $proto_dirs; do
# generate swagger files (filter query files)
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
if [[ ! -z "$query_file" ]]; then
buf alpha protoc \
-I="$PROJECTDIR/proto" \
-I="$IBC_DIR/proto" \
-I="$COSMOS_SDK_DIR/third_party/proto" \
-I="$COSMOS_SDK_DIR/proto" \
--gocosmos_out=Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types,Mgoogle/protobuf/empty.proto=github.com/gogo/protobuf/types,plugins=interfacetype+grpc,paths=source_relative:$COSMOS_SDK_DIR \
--swagger_out=$GEN_DIR \
--swagger_opt=logtostderr=true,fqn_for_swagger_name=true,simple_operation_ids=true \
$query_file

fi
done

if [ $(uname) == 'Darwin' ]
then
sed -i '' 's/UpgradedConsensusState/UpgradedIBCConsensusState/' $GEN_DIR/ibc/core/client/v1/query.swagger.json
else
# fix for darwin
sed -i 's/UpgradedConsensusState/UpgradedIBCConsensusState/' $GEN_DIR/ibc/core/client/v1/query.swagger.json
fi

swagger_files=$(find $GEN_DIR/gravity $GEN_DIR/ibc -name 'query.swagger.json' | xargs)

node -e "var fs = require('fs'),file='$COSMOS_SDK_DIR/client/docs/config.json',result = fs.readFileSync(file).toString().replace('./client','$COSMOS_SDK_DIR/client').replace(/.\/tmp-swagger-gen/g, '$GEN_DIR');
var swaggerFiles = '$swagger_files'.split(' '), obj = JSON.parse(result);
swaggerFiles.forEach(swaggerFile => {
obj.apis.push({
url: swaggerFile,
operationIds: {
rename: {
Params: swaggerFile.split('/').slice(3,-2).map(dir=>dir[0].toUpperCase() + dir.slice(1)).join('') + 'Params'
}
}
});
});
fs.writeFileSync('$GEN_DIR/config.json', JSON.stringify(obj, null, 2));
"


# combine swagger files
# all the individual swagger files need to be configured in `config.json` for merging
swagger-combine $GEN_DIR/config.json -o $DOC_DIR/swagger-ui/swagger.yaml -f yaml --continueOnConflictingPaths true --includeDefinitions true


# make statik
# statik -src doc/swagger-ui/ -dest doc -f
14 changes: 14 additions & 0 deletions module/doc/statik/statik.go

Large diffs are not rendered by default.

Binary file added module/doc/swagger-ui/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added module/doc/swagger-ui/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions module/doc/swagger-ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="swagger-ui.css" >
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>

<script src="swagger-ui-bundle.js"> </script>
<script src="swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {

// Build a system
const ui = SwaggerUIBundle({
url: "./swagger.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui
}
</script>
</body>
</html>
67 changes: 67 additions & 0 deletions module/doc/swagger-ui/oauth2-redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html lang="en-US">
<body onload="run()">
</body>
</html>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;

if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1);
} else {
qp = location.search.substring(1);
}

arr = qp.split("&")
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value)
}
) : {}

isValid = qp.state === sentState

if ((
oauth2.auth.schema.get("flow") === "accessCode"||
oauth2.auth.schema.get("flow") === "authorizationCode"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
});
}

if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}

oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}
</script>
Loading

0 comments on commit 6a3e980

Please sign in to comment.