Skip to content

Commit

Permalink
Expose controls of SwaggerUIBundle JS application (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 15, 2021
1 parent 26d722c commit 0a55191
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 148 deletions.
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
2 changes: 1 addition & 1 deletion internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
94 changes: 52 additions & 42 deletions internal/index.tpl.go
Original file line number Diff line number Diff line change
@@ -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 `
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -37,52 +87,12 @@ func IndexTpl(assetsBase, faviconBase string) string {
<script src="` + assetsBase + `swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function () {
var cfg = {
"title": "API Document",
"swaggerJsonUrl": "/swagger.json",
"basePath": "/",
"showTopBar": false,
"jsonEditor": false,
"preAuthorizeApiKey": null
};
var cfg = {{ .ConfigJson }};
var url = window.location.protocol + "//" + window.location.host + cfg.swaggerJsonUrl;
// Build a system
var settings = {
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));
}
}
}
` + strings.Join(settingsStr, ",\n") + `
};
if (cfg.showTopBar == false) {
Expand Down
105 changes: 0 additions & 105 deletions v3cdn/index.tpl.go

This file was deleted.

0 comments on commit 0a55191

Please sign in to comment.