Skip to content

Commit

Permalink
Fix handling of base path without trailing backslash
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Oct 10, 2020
1 parent 55b73eb commit 4facfd7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion v3/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import "net/http"
var staticServer http.Handler

const (
assetsBase = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.23.6/"
assetsBase = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.35.1/"
faviconBase = "https://petstore.swagger.io/"
)
15 changes: 9 additions & 6 deletions v3/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"html/template"
"net/http"
"strings"

"github.com/swaggest/swgui"
)

// Handler handle swagger UI request
// Handler handles swagger UI request.
type Handler struct {
swgui.Config

Expand All @@ -18,12 +19,12 @@ type Handler struct {
staticServer http.Handler
}

// NewHandler returns a HTTP handler for swagger UI
// NewHandler returns a HTTP handler for swagger UI.
func NewHandler(title, swaggerJSONPath string, basePath string) *Handler {
h := &Handler{}
h.Title = title
h.SwaggerJSON = swaggerJSONPath
h.BasePath = basePath
h.BasePath = strings.TrimSuffix(basePath, "/") + "/"

j, _ := json.Marshal(h.Config)
h.ConfigJson = template.JS(j)
Expand All @@ -34,8 +35,10 @@ func NewHandler(title, swaggerJSONPath string, basePath string) *Handler {
return h
}

// NewHandlerWithConfig returns a HTTP handler for swagger UI
// NewHandlerWithConfig returns a HTTP handler for swagger UI.
func NewHandlerWithConfig(config swgui.Config) *Handler {
config.BasePath = strings.TrimSuffix(config.BasePath, "/") + "/"

h := &Handler{
Config: config,
}
Expand All @@ -48,9 +51,9 @@ func NewHandlerWithConfig(config swgui.Config) *Handler {
return h
}

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

0 comments on commit 4facfd7

Please sign in to comment.