Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/labstack/echo/v4"
"github.com/swaggo/echo-swagger"
echoSwagger "github.com/swaggo/echo-swagger"
_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.
)

Expand All @@ -23,12 +23,11 @@ import (
func main() {
e := echo.New()

e.GET("/swagger/*", echoSwagger.WrapHandler)
echoSwagger.RegisterEchoHandler(e.GET, "/swagger/", nil)

/*
Or can use EchoWrapHandler func with configurations.
url := echoSwagger.URL("http://localhost:1323/swagger/doc.json") //The url pointing to API definition
e.GET("/swagger/*", echoSwagger.EchoWrapHandler(url))
Or can use customized configurations.
echoSwagger.RegisterEchoHandler(e.GET, "/swagger/", echoSwagger.NewConfig().WithURL("doc.json"))
*/
e.Logger.Fatal(e.Start(":1323"))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/swaggo/gin-swagger v1.0.0
github.com/swaggo/swag v1.4.0
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 // indirect
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952 // indirect
golang.org/x/tools v0.0.0-20190205050122-7f7074d5bcfd // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-openapi/jsonpointer v0.17.0 h1:nH6xp8XdXHx8dqveo0ZuJBluCO2qGrPbDNZ0dwoRHP0=
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
Expand Down
78 changes: 40 additions & 38 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,75 @@ package echoSwagger

import (
"html/template"
"net/http"
"regexp"

"github.com/labstack/echo/v4"
"github.com/swaggo/files"
swaggerFiles "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
url string
// Prefix is the URL path prefix to strip from WebDAV resource paths.
prefix string
// //The path pointing to API definition file(normally swagger.json or swagger.yaml).
// docPath string
// // The server host
// host string
}

// 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
// NewConfig create a default config
func NewConfig() *Config {
return &Config{
url: "doc.json",
prefix: "/",
}
}

// WrapHandler wraps swaggerFiles.Handler and returns echo.HandlerFunc
var WrapHandler = EchoWrapHandler()

// EchoWrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc {
// WithURL set the URL
func (c *Config) WithURL(url string) *Config {
c.url = url
return c
}

handler := swaggerFiles.Handler
//URL pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
func (c *Config) URL() string {
return c.prefix + c.url
}

config := &Config{
URL: "doc.json",
// RegisterEchoHandler for prefix
func RegisterEchoHandler(method func(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route, prefix string, config *Config, m ...echo.MiddlewareFunc) {
if config == nil {
config = NewConfig()
}
config.prefix = prefix

for _, c := range confs {
c(config)
method(prefix+"*", EchoWrapHandler(config), m...)
}

// EchoWrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
func EchoWrapHandler(config *Config) echo.HandlerFunc {
if config == nil {
config = NewConfig()
}

handler := swaggerFiles.Handler
handler.Prefix = config.prefix

// create a template with name
t := template.New("swagger_index.html")
index, _ := t.Parse(indexTempl)

type pro struct {
Host string
}

var re = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`)

return func(c echo.Context) error {
var matches []string
if matches = re.FindStringSubmatch(c.Request().RequestURI); len(matches) != 3 {

return c.String(http.StatusNotFound, "404 page not found")
}
path := matches[2]
prefix := matches[1]
handler.Prefix = prefix

switch path {
case "index.html":

switch c.Request().URL.Path {
case config.prefix, config.prefix + "index.html":
index.Execute(c.Response().Writer, config)
case "doc.json":
case config.prefix + "doc.json":
doc, _ := swag.ReadDoc()
c.Response().Write([]byte(doc))
default:
handler.ServeHTTP(c.Response().Writer, c.Request())

}

return nil
Expand Down
29 changes: 18 additions & 11 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ import (
)

func TestWrapHandler(t *testing.T) {
for _, prefix := range []string{"/", "/swagger/"} {
router := echo.New()
RegisterEchoHandler(router.GET, prefix, nil)

router := echo.New()
w1 := performRequest("GET", prefix+"index.html", router)
assert.Equal(t, 200, w1.Code, prefix)
indexContent := w1.Body.Bytes()

router.GET("/*", WrapHandler)
w1_2 := performRequest("GET", prefix+"", router)
assert.Equal(t, 200, w1_2.Code, prefix)
assert.Equal(t, indexContent, w1_2.Body.Bytes())

w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
w2 := performRequest("GET", prefix+"doc.json", router)
assert.Equal(t, 200, w2.Code, prefix)

w2 := performRequest("GET", "/doc.json", router)
assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", prefix+"favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code, prefix)

w3 := performRequest("GET", "/favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code)

w4 := performRequest("GET", "/notfound", router)
assert.Equal(t, 404, w4.Code)
w3_1 := performRequest("GET", prefix+"swagger-ui.css", router)
assert.Equal(t, 200, w3_1.Code, prefix)

w4 := performRequest("GET", prefix+"notfound", router)
assert.Equal(t, 404, w4.Code, prefix)
}
}

func performRequest(method, target string, e *echo.Echo) *httptest.ResponseRecorder {
Expand Down