Skip to content

Commit

Permalink
Merge PR cosmos#4413: Revert swagger removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sabau authored and alexanderbez committed May 28, 2019
1 parent 73e5ef7 commit c39fa75
Show file tree
Hide file tree
Showing 15 changed files with 57,582 additions and 24 deletions.
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
command: |
make runsim tools TOOLS_DESTDIR=/tmp/workspace/bin
cp $GOPATH/bin/runsim /tmp/workspace/bin
cp $GOPATH/bin/statik /tmp/workspace/bin
- run:
name: cache go modules
command: |
Expand Down Expand Up @@ -240,10 +241,37 @@ jobs:
echo "Website build started"
fi
check_statik:
<<: *linux_defaults
parallelism: 1
steps:
- attach_workspace:
at: /tmp/workspace
- checkout
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
- run:
name: check if statik.go is up-to-date
command: |
set -ex
export PATH=/tmp/workspace/bin:$PATH
make update-swagger-docs
if [ -n "$(git status --porcelain)" ]; then
echo "swagger docs out of sync";
exit 1
else
echo "swagger docs are in sync";
fi
workflows:
version: 2
test-suite:
jobs:
- check_statik:
requires:
- setup_dependencies
- deploy_docs:
filters:
branches:
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ ci: tools build test_cover lint test
build: go.sum
@go build -mod=readonly ./...

update-swagger-docs:
@statik -src=client/lcd/swagger-ui -dest=client/lcd -f -m

dist:
@bash publish/dist.sh
@bash publish/publish.sh
Expand Down
15 changes: 15 additions & 0 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package lcd
import (
"fmt"
"net"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -17,6 +19,9 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
keybase "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/server"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/lcd/statik"
)

// RestServer represents the Light Client Rest server
Expand Down Expand Up @@ -83,6 +88,7 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C
rs := NewRestServer(cdc)

registerRoutesFn(rs)
rs.registerSwaggerUI()

// Start the rest server and return error if one exists
err = rs.Start(
Expand All @@ -98,3 +104,12 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C

return flags.RegisterRestServerFlags(cmd)
}

func (rs *RestServer) registerSwaggerUI() {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
rs.Mux.PathPrefix("/swagger-ui/").Handler(http.StripPrefix("/swagger-ui/", staticServer))
}
3 changes: 3 additions & 0 deletions client/lcd/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/lcd/statik
13 changes: 13 additions & 0 deletions client/lcd/statik/statik.go

Large diffs are not rendered by default.

Binary file added client/lcd/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 client/lcd/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 client/lcd/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 client/lcd/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 c39fa75

Please sign in to comment.