Skip to content
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

[main]: support for plugins and multiple swagger hosting (bonus - bug fixed) #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type uiConfig struct {
oauth2RedirectUrl configValue[string]
maxDisplayedTags configValue[int]
validatorUrl configValue[string]
plugins []string
}

type DocExpansion string
Expand Down Expand Up @@ -72,6 +73,10 @@ type SpecURL struct {
URL string `json:"url"`
}

var (
PluginTopBar = "SwaggerUIBundle.plugins.TopBar"
)

// Option is a function that takes a pointer to uiConfig and modifies it.
type Option func(*uiConfig)

Expand Down Expand Up @@ -338,3 +343,11 @@ func WithBasePath(basePath string) Option {
cfg.basePath = strings.TrimSuffix(basePath, "/") + "/"
}
}

// WithPlugins adds plugins to config.
// For now only PluginTopBar is supported
func WithPlugins(plugins ...string) Option {
return func(config *uiConfig) {
config.plugins = append(config.plugins, plugins...)
}
}
16 changes: 14 additions & 2 deletions examples/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ package main

import (
"fmt"
swaggerui "github.com/alexliesenfeld/go-swagger-ui"
"log"
"net/http"

swaggerui "github.com/alexliesenfeld/go-swagger-ui"
)

func main() {
http.HandleFunc("/", swaggerui.NewHandler(
swaggerui.WithHTMLTitle("My Example Petstore API"),
swaggerui.WithSpecURL("https://petstore.swagger.io/v2/swagger.json"),
swaggerui.WithSpecURLs("One", []swaggerui.SpecURL{
{
Name: "One",
URL: "https://petstore.swagger.io/v2/swagger.json",
},
{
Name: "Two",
URL: "https://petstore.swagger.io/v2/swagger.json",
},
}),
swaggerui.WithLayout(swaggerui.LayoutStandaloneLayout),
swaggerui.WithDocExpansion(swaggerui.DocExpansionFull),
swaggerui.WithPlugins(swaggerui.PluginTopBar),
))

fmt.Println("Starting Swagger UI on http://localhost:8080")
Expand Down
5 changes: 3 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func replaceVars(w io.Writer, tpl *template.Template, cfg *uiConfig) error {
DefaultModelRendering, QueryConfigEnabled, SupportedSubmitMethods, DeepLinking,
ShowMutatedRequest, ShowExtensions, ShowCommonExtensions, Filter, FilterString,
DisplayOperationId, TryItOutEnabled, DisplayRequestDuration, PersistAuthorization, WithCredentials,
OAuth2RedirectUrl, Layout, ValidatorURL, MaxDisplayedTags, PrimaryURL, ConfigURL, URLs string
OAuth2RedirectUrl, Layout, ValidatorURL, MaxDisplayedTags, PrimaryURL, ConfigURL, URLs, Plugins string
}{
BasePath: cfg.basePath,
ConfigURL: fromStringConfigValue(cfg.configURL),
Expand All @@ -146,11 +146,12 @@ func replaceVars(w io.Writer, tpl *template.Template, cfg *uiConfig) error {
PersistAuthorization: fromBoolConfigValue(cfg.persistAuthorization),
WithCredentials: fromBoolConfigValue(cfg.withCredentials),
OAuth2RedirectUrl: fromStringConfigValue(cfg.oauth2RedirectUrl),
Layout: fromStringConfigValue(cfg.oauth2RedirectUrl),
Layout: fromStringConfigValue(cfg.layout),
ValidatorURL: fromStringConfigValue(cfg.validatorUrl),
MaxDisplayedTags: fromIntConfigValue(cfg.maxDisplayedTags),
PrimaryURL: fromStringConfigValue(cfg.urlsPrimary),
URLs: urlsAsBase64EncodedJSON,
Plugins: strings.TrimSpace(strings.Join(cfg.plugins, ",")),
})
}

Expand Down
135 changes: 133 additions & 2 deletions swagger-ui/dist/swagger-ui-bundle.js

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions swagger-ui/dist/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion swagger-ui/templates/swagger-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ window.onload = function() {
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
SwaggerUIBundle.plugins.DownloadUrl,
SwaggerUIBundle.plugins.TopBar,
// ...blankToPluginList('{{ .Plugins }}'),
],
configUrl: blankToUndefined('{{ .ConfigURL }}'),
spec: parseJson(decodeBase64(blankToUndefined('{{ .Spec }}'))),
Expand Down Expand Up @@ -110,3 +112,22 @@ function parseJson(str) {
return JSON.parse(str);
}

function blankToPluginList(list) {
const pluginNames = blankToUndefinedArray(list)
if (!pluginNames) {
return []
}

const plugins = []

for (const i in pluginNames) {

switch (pluginNames[i]) {
case 'SwaggerUIBundle.plugins.TopBar':
plugins.push(SwaggerUIBundle.plugins.TopBar)
}
}

return plugins
}