Skip to content

Commit

Permalink
Add internal basepath (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanh authored Jan 20, 2024
1 parent 49da35b commit d5c2e19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Config struct {
SwaggerJSON string `json:"swaggerJsonUrl"` // URL to openapi.json/swagger.json document specification.
BasePath string `json:"basePath"` // Base URL to docs.

// InternalBasePath is used to override BasePath if external
// url differs from internal one.
InternalBasePath string `json:"-"`

ShowTopBar bool `json:"showTopBar"` // Show navigation top bar, hidden by default.
HideCurl bool `json:"hideCurl"` // Hide curl code snippet.
JsonEditor bool `json:"jsonEditor"` // Enable visual json editor support (experimental, can fail with complex schemas).
Expand Down
10 changes: 8 additions & 2 deletions internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, s
Config: config,
}

if h.InternalBasePath == "" {
h.InternalBasePath = h.BasePath
}

h.InternalBasePath = strings.TrimSuffix(h.InternalBasePath, "/") + "/"

j, err := json.Marshal(h.Config)
if err != nil {
panic(err)
Expand All @@ -41,15 +47,15 @@ func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, s
}

if staticServer != nil {
h.staticServer = http.StripPrefix(h.BasePath, staticServer)
h.staticServer = http.StripPrefix(h.InternalBasePath, staticServer)
}

return h
}

// ServeHTTP implements http.Handler interface to handle swagger UI request.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(h.BasePath, "/") && h.staticServer != nil {
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(h.InternalBasePath, "/") && h.staticServer != nil {
h.staticServer.ServeHTTP(w, r)

return
Expand Down

0 comments on commit d5c2e19

Please sign in to comment.