-
Notifications
You must be signed in to change notification settings - Fork 54
Serve swagger ui from embedded resources #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f908e4a
a7da07d
72368d2
352a3df
bd21a49
0dd9930
45b7157
38708b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
go: | ||
- "1.15.x" | ||
- "1.16.x" | ||
- "1.17.x" | ||
os: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/wI2L/fizz | ||
|
||
go 1.12 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/Pallinder/go-randomdata v1.2.0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Serve Swagger UI | ||
|
||
## Updating the Swagger UI assets | ||
|
||
1. Copy all assets from [Swagger-ui repo](https://github.com/swagger-api/swagger-ui/tree/master/dist) to `swagger-ui` directory. | ||
2. Rename `index.html` to `index.gothml` | ||
3. In `index.gohtml` set `url` of `SwaggerUIBundle` to `{{ .specPath }}` | ||
|
||
Done 👍 | ||
|
||
Now execute tests and make sure they pass. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package swagger | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"io" | ||
"io/fs" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type fsWrapper struct { | ||
fs fs.FS | ||
specPath string | ||
} | ||
|
||
func newFsWrapper(fs fs.FS, specPath string) *fsWrapper { | ||
return &fsWrapper{ | ||
fs: fs, | ||
specPath: specPath, | ||
} | ||
} | ||
|
||
func (f *fsWrapper) Open(name string) (fs.File, error) { | ||
if strings.EqualFold(name, "index.html") { | ||
tpl, err := template.ParseFS(f.fs, "index.gohtml") | ||
if err != nil { | ||
return nil, err | ||
} | ||
data := map[string]interface{}{ | ||
"specPath": f.specPath, | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
if err = tpl.Execute(buf, data); err != nil { | ||
return nil, err | ||
} | ||
|
||
return fsWrapperFile{ | ||
r: io.NopCloser(bytes.NewReader(buf.Bytes())), | ||
fInfo: fileInfoMock{ | ||
name: name, | ||
size: int64(buf.Len()), | ||
}, | ||
}, nil | ||
} | ||
|
||
return f.fs.Open(name) | ||
} | ||
|
||
type fsWrapperFile struct { | ||
r io.ReadCloser | ||
fInfo fs.FileInfo | ||
} | ||
|
||
func (f fsWrapperFile) Stat() (fs.FileInfo, error) { | ||
return f.fInfo, nil | ||
} | ||
|
||
func (f fsWrapperFile) Read(bytes []byte) (int, error) { | ||
return f.r.Read(bytes) | ||
} | ||
|
||
func (f fsWrapperFile) Close() error { | ||
return f.r.Close() | ||
} | ||
|
||
type fileInfoMock struct { | ||
name string | ||
size int64 | ||
} | ||
|
||
func (f fileInfoMock) Name() string { | ||
return f.name | ||
} | ||
|
||
func (f fileInfoMock) Size() int64 { | ||
return f.size | ||
} | ||
|
||
func (f fileInfoMock) Mode() fs.FileMode { | ||
return fs.FileMode(0) | ||
} | ||
|
||
func (f fileInfoMock) ModTime() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (f fileInfoMock) IsDir() bool { | ||
return false | ||
} | ||
|
||
func (f fileInfoMock) Sys() interface{} { | ||
panic("implement me") | ||
} |
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" charset="UTF-8"> </script> | ||
<script src="swagger-ui-standalone-preset.js" charset="UTF-8"> </script> | ||
<script> | ||
window.onload = function() { | ||
// Begin Swagger UI call region | ||
const ui = SwaggerUIBundle({ | ||
url: "{{ .specPath }}", | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout" | ||
}); | ||
// End Swagger UI call region | ||
|
||
window.ui = ui; | ||
}; | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<!doctype html> | ||
<html lang="en-US"> | ||
<head> | ||
<title>Swagger UI: OAuth2 Redirect</title> | ||
</head> | ||
<body> | ||
<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.schema.get("flow") === "authorization_code" | ||
) && !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(); | ||
} | ||
|
||
window.addEventListener('DOMContentLoaded', function () { | ||
run(); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package swagger | ||
|
||
import ( | ||
"embed" | ||
"github.com/gin-gonic/gin" | ||
"io/fs" | ||
"net/http" | ||
) | ||
|
||
//go:embed swagger-ui | ||
var swaggerUiRes embed.FS | ||
|
||
// AddUIHandler adds handler that serves html for Swagger UI | ||
func AddUIHandler(ginEngine *gin.Engine, path string, openApiSpecPath string) error { | ||
sub, err := fs.Sub(swaggerUiRes, "swagger-ui") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ginEngine.StaticFS(path, http.FS(newFsWrapper(sub, openApiSpecPath))) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package swagger | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/wI2L/fizz" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestAddUIHandler(t *testing.T) { | ||
const testPath = "/test/swagger" | ||
f := fizz.New() | ||
err := AddUIHandler(f.Engine(), testPath, "openapi.json") | ||
|
||
assert.NoError(t, err) | ||
|
||
srv := httptest.NewServer(f) | ||
defer srv.Close() | ||
resp, err := srv.Client().Get(srv.URL + testPath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
body := new(strings.Builder) | ||
_, err = io.Copy(body, resp.Body) | ||
assert.Contains(t, body.String(), "openapi.json") | ||
assert.Contains(t, body.String(), "<title>Swagger UI</title>") | ||
} |
Uh oh!
There was an error while loading. Please reload this page.