core/internal/port/internal/swgui/swgui.go

48 lines
1.0 KiB
Go

package swgui
import (
"embed"
"fmt"
"net/http"
)
//go:embed *.png *.js *.html *.css
var fs embed.FS
const initializerTpl = `
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">
window.ui = SwaggerUIBundle({
url: "%s",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
//</editor-fold>
};
`
func Handler(basePath string, openAPISchemaURL string) http.Handler {
fileServer := http.StripPrefix(basePath, http.FileServer(http.FS(fs)))
swaggerInitializer := []byte(fmt.Sprintf(initializerTpl, openAPISchemaURL))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == basePath+"/swagger-initializer.js" {
w.Header().Set("Content-Type", "text/javascript; charset=utf-8")
_, _ = w.Write(swaggerInitializer)
return
}
fileServer.ServeHTTP(w, r)
})
}