From bea1fa4b84f96ee6315a0a2c6a3f88f37e9f9f84 Mon Sep 17 00:00:00 2001 From: "webee.yw" Date: Wed, 3 Apr 2019 11:28:14 +0800 Subject: [PATCH 1/2] support root path as index page --- swagger.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/swagger.go b/swagger.go index df41455..8f3300d 100644 --- a/swagger.go +++ b/swagger.go @@ -47,7 +47,7 @@ func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc { 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)[\?|.]*`) + 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 @@ -60,8 +60,7 @@ func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc { handler.Prefix = prefix switch path { - case "index.html": - + case "/", "index.html": index.Execute(c.Response().Writer, config) case "doc.json": doc, _ := swag.ReadDoc() From aed4c942c47acecba9c8a4de36251e34183608b1 Mon Sep 17 00:00:00 2001 From: "webee.yw" Date: Tue, 9 Jul 2019 19:16:57 +0800 Subject: [PATCH 2/2] update --- example/main.go | 9 +++--- go.mod | 2 +- go.sum | 1 + swagger.go | 77 +++++++++++++++++++++++++------------------------ swagger_test.go | 29 ++++++++++++------- 5 files changed, 64 insertions(+), 54 deletions(-) diff --git a/example/main.go b/example/main.go index 8586acf..7bceba3 100644 --- a/example/main.go +++ b/example/main.go @@ -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. ) @@ -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")) } diff --git a/go.mod b/go.mod index 8cfd7ea..84d1ad0 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 95b25b2..d436956 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/swagger.go b/swagger.go index 8f3300d..3959ed5 100644 --- a/swagger.go +++ b/swagger.go @@ -2,72 +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 diff --git a/swagger_test.go b/swagger_test.go index f361dcc..d44a47c 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -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 {