From 0a55191034e4f7f2eac60c928f55ab4cadcbbdde Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Tue, 15 Jun 2021 14:02:59 +0200 Subject: [PATCH] Expose controls of SwaggerUIBundle JS application (#22) --- config.go | 5 ++ internal/handler.go | 2 +- internal/index.tpl.go | 94 ++++++++++++++++++++----------------- v3cdn/index.tpl.go | 105 ------------------------------------------ 4 files changed, 58 insertions(+), 148 deletions(-) delete mode 100644 v3cdn/index.tpl.go diff --git a/config.go b/config.go index 57614f2..78513b4 100644 --- a/config.go +++ b/config.go @@ -9,4 +9,9 @@ type Config struct { ShowTopBar bool `json:"showTopBar"` // Show navigation top bar, hidden by default JsonEditor bool `json:"jsonEditor"` // Enable visual json editor support (experimental, can fail with complex schemas) PreAuthorizeApiKey map[string]string `json:"preAuthorizeApiKey"` // Map of security name to key value + + // SettingsUI contains keys and plain javascript values of SwaggerUIBundle configuration. + // Overrides default values. + // See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ for available options. + SettingsUI map[string]string `json:"-"` } diff --git a/internal/handler.go b/internal/handler.go index 1da0f06..c57bac5 100644 --- a/internal/handler.go +++ b/internal/handler.go @@ -28,7 +28,7 @@ func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, s } j, _ := json.Marshal(h.Config) h.ConfigJson = template.JS(j) - h.tpl, _ = template.New("index").Parse(IndexTpl(assetsBase, faviconBase)) + h.tpl, _ = template.New("index").Parse(IndexTpl(assetsBase, faviconBase, config)) if staticServer != nil { h.staticServer = http.StripPrefix(h.BasePath, staticServer) } diff --git a/internal/index.tpl.go b/internal/index.tpl.go index 8b3b1df..ad36e42 100644 --- a/internal/index.tpl.go +++ b/internal/index.tpl.go @@ -1,6 +1,56 @@ package internal -func IndexTpl(assetsBase, faviconBase string) string { +import ( + "github.com/swaggest/swgui" + "sort" + "strings" +) + +func IndexTpl(assetsBase, faviconBase string, cfg swgui.Config) string { + settings := map[string]string{ + "url": "url", + "dom_id": "'#swagger-ui'", + "deepLinking": "true", + "presets": `[ + SwaggerUIBundle.presets.apis, + SwaggerUIStandalonePreset + ]`, + "plugins": `[ + SwaggerUIBundle.plugins.DownloadUrl + ]`, + "layout": `"StandaloneLayout"`, + "showExtensions": "true", + "showCommonExtensions": "true", + "validatorUrl": "null", + `onComplete`: `function() { + if (cfg.preAuthorizeApiKey) { + for (var name in cfg.preAuthorizeApiKey) { + ui.preauthorizeApiKey(name, cfg.preAuthorizeApiKey[name]); + } + } + + var dom = document.querySelector('.scheme-container select'); + for (var key in dom) { + if (key.startsWith("__reactInternalInstance$")) { + var compInternals = dom[key]._currentElement; + var compWrapper = compInternals._owner; + compWrapper._instance.setScheme(window.location.protocol.slice(0,-1)); + } + } + }`, + } + + for k, v := range cfg.SettingsUI { + settings[k] = v + } + + var settingsStr []string + for k, v := range settings { + settingsStr = append(settingsStr, "\t\t\t"+k+": "+v) + } + + sort.Strings(settingsStr) + return ` @@ -37,52 +87,12 @@ func IndexTpl(assetsBase, faviconBase string) string { - - - - -`