-
Notifications
You must be signed in to change notification settings - Fork 58
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 4 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 |
|---|---|---|
| @@ -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,95 @@ | ||
| package swagger | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "html/template" | ||
| "io" | ||
| "io/fs" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| type fsWrapper struct { | ||
| fs fs.FS | ||
| jsonFileName string | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func FsWrapper(fs fs.FS, jsonFilePath string) *fsWrapper { | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| return &fsWrapper{ | ||
| fs: fs, | ||
| jsonFileName: jsonFilePath, | ||
| } | ||
| } | ||
|
|
||
| func (f *fsWrapper) Open(name string) (fs.File, error) { | ||
| if strings.HasSuffix(name, "index.html") { | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| tpl, err := template.ParseFS(f.fs, "index.gohtml") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| data := map[string]interface{}{ | ||
| "openApiJson": strings.TrimPrefix(f.jsonFileName, "/"), | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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: "/{{ .openApiJson }}", | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| 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,21 @@ | ||
| 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, openApiJsonPath string) { | ||
| sub, err := fs.Sub(swaggerUiRes, "swagger-ui") | ||
| if err != nil { | ||
| panic(err) | ||
|
DenisPalnitsky marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| ginEngine.StaticFS(path, http.FS(FsWrapper(sub, openApiJsonPath))) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.