Skip to content

Commit

Permalink
Fix link to Swagger spec in Swagger UI by reworking URL config.
Browse files Browse the repository at this point in the history
This makes it more clear that this config option should only be set if
the Swagger spec document is not expected to be hosted by this
middleware but instead by an external website or service. It also fixes
the link to the JSON document under the base path on the Swagger UI.

Also set the swaggerFiles handler prefix again, as this is required.
  • Loading branch information
drewsilcock committed Jan 6, 2020
1 parent b2ab74f commit 57d78e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
48 changes: 33 additions & 15 deletions swagger.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package echoSwagger

import (
"github.com/labstack/echo/v4"
"github.com/swaggo/files"
"github.com/swaggo/swag"
"html/template"
"log"
"net/http"
"path"

"github.com/labstack/echo/v4"
"github.com/swaggo/files"
"github.com/swaggo/swag"
)

// Config stores echoSwagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
// If the Swagger specification JSON document is external to the web server, this should be set to the full URL
// to the specification document.
ExternalURL *string

// The information for OAuth2 integration, if any.
OAuth *OAuthConfig
Expand All @@ -33,10 +33,15 @@ type OAuthConfig struct {
AppName string
}

type templateData struct {
SwaggerSpecFullURL string
Config
}

// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
func URL(url string) func(c *Config) {
return func(c *Config) {
c.URL = url
c.ExternalURL = &url
}
}

Expand All @@ -48,9 +53,7 @@ func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc {

handler := swaggerFiles.Handler

config := &Config{
URL: "doc.json",
}
config := &Config{}

for _, c := range confs {
c(config)
Expand All @@ -64,20 +67,35 @@ func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc {
}

return func(c echo.Context) error {
_, path := path.Split(c.Request().RequestURI)
prefix, pathFile := path.Split(c.Request().RequestURI)
handler.Prefix = prefix

templateData := templateData{
SwaggerSpecFullURL: path.Join(prefix, "doc.json"),
Config: *config,
}

if config.ExternalURL != nil {
// Configuration specifies override for Swagger specification URL.
templateData.SwaggerSpecFullURL = *config.ExternalURL
}

switch path {
switch pathFile {
case "":
fallthrough
case "index.html":
index.Execute(c.Response().Writer, config)
if err := index.Execute(c.Response().Writer, templateData); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
case "doc.json":
doc, err := swag.ReadDoc()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
c.Response().Header().Set("Content-Type", "application/json")
c.Response().Write([]byte(doc))
if _, err := c.Response().Write([]byte(doc)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
default:
handler.ServeHTTP(c.Response().Writer, c.Request())
}
Expand Down Expand Up @@ -161,7 +179,7 @@ const indexTempl = `<!-- HTML for static distribution bundle build -->
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "{{.URL}}",
url: "{{.SwaggerSpecFullURL}}",
dom_id: '#swagger-ui',
validatorUrl: null,
presets: [
Expand Down
8 changes: 3 additions & 5 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ func TestWrapHandler(t *testing.T) {
func TestConfig(t *testing.T) {
router := echo.New()

swaggerHandler := EchoWrapHandler(func(c *Config) {
c.URL = "example.org/swagger.json"
})
router.GET("/*", swaggerHandler)
swaggerHandler := URL("http://example.org/swagger.json")
router.GET("/*", EchoWrapHandler(swaggerHandler))

w := performRequest("GET", "/", router)
assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "url: \"example.org/swagger.json\"")
assert.Contains(t, w.Body.String(), "url: \"http:\\/\\/example.org\\/swagger.json\"")
}

func TestConfigWithOAuth(t *testing.T) {
Expand Down

0 comments on commit 57d78e6

Please sign in to comment.