Skip to content

Commit d5c2e19

Browse files
authored
Add internal basepath (#44)
1 parent 49da35b commit d5c2e19

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ type Config struct {
66
SwaggerJSON string `json:"swaggerJsonUrl"` // URL to openapi.json/swagger.json document specification.
77
BasePath string `json:"basePath"` // Base URL to docs.
88

9+
// InternalBasePath is used to override BasePath if external
10+
// url differs from internal one.
11+
InternalBasePath string `json:"-"`
12+
913
ShowTopBar bool `json:"showTopBar"` // Show navigation top bar, hidden by default.
1014
HideCurl bool `json:"hideCurl"` // Hide curl code snippet.
1115
JsonEditor bool `json:"jsonEditor"` // Enable visual json editor support (experimental, can fail with complex schemas).

internal/handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, s
2828
Config: config,
2929
}
3030

31+
if h.InternalBasePath == "" {
32+
h.InternalBasePath = h.BasePath
33+
}
34+
35+
h.InternalBasePath = strings.TrimSuffix(h.InternalBasePath, "/") + "/"
36+
3137
j, err := json.Marshal(h.Config)
3238
if err != nil {
3339
panic(err)
@@ -41,15 +47,15 @@ func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, s
4147
}
4248

4349
if staticServer != nil {
44-
h.staticServer = http.StripPrefix(h.BasePath, staticServer)
50+
h.staticServer = http.StripPrefix(h.InternalBasePath, staticServer)
4551
}
4652

4753
return h
4854
}
4955

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

5561
return

0 commit comments

Comments
 (0)