diff --git a/.gitignore b/.gitignore index 8c66381..a668388 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,8 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -dist \ No newline at end of file +dist + +.idea +vendor +.envrc diff --git a/example/api/api.go b/example/basic/api/api.go similarity index 100% rename from example/api/api.go rename to example/basic/api/api.go diff --git a/example/docs/docs.go b/example/basic/docs/docs.go similarity index 100% rename from example/docs/docs.go rename to example/basic/docs/docs.go diff --git a/example/main.go b/example/basic/main.go similarity index 93% rename from example/main.go rename to example/basic/main.go index ea5536c..05052ec 100644 --- a/example/main.go +++ b/example/basic/main.go @@ -5,7 +5,7 @@ import ( "github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger/swaggerFiles" - _ "github.com/swaggo/gin-swagger/example/docs" + _ "github.com/swaggo/gin-swagger/example/basic/docs" ) // @title Swagger Example API diff --git a/example/web/handler.go b/example/basic/web/handler.go similarity index 100% rename from example/web/handler.go rename to example/basic/web/handler.go diff --git a/example/gzipped/main.go b/example/gzipped/main.go new file mode 100644 index 0000000..8e4463e --- /dev/null +++ b/example/gzipped/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/gin-contrib/gzip" + "github.com/gin-gonic/gin" + "github.com/swaggo/gin-swagger" + "github.com/swaggo/gin-swagger/swaggerFiles" + + _ "github.com/swaggo/gin-swagger/example/basic/docs" +) + +// @title Swagger Example API +// @version 1.0 +// @description This is a sample server Petstore server. +// @termsOfService http://swagger.io/terms/ + +// @contact.name API Support +// @contact.url http://www.swagger.io/support +// @contact.email support@swagger.io + +// @license.name Apache 2.0 +// @license.url http://www.apache.org/licenses/LICENSE-2.0.html + +// @host petstore.swagger.io +// @BasePath /v2 +func main() { + r := gin.New() + + r.Use(gzip.Gzip(gzip.BestSpeed)) + + url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") //The url pointing to API definition + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) + + r.Run() +} diff --git a/go.mod b/go.mod index 8137c3b..eabf6fa 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,11 @@ module github.com/swaggo/gin-swagger require ( - github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect + github.com/gin-contrib/gzip v0.0.1 github.com/gin-gonic/gin v1.3.0 github.com/go-openapi/jsonreference v0.18.0 // indirect github.com/go-openapi/spec v0.18.0 // indirect - github.com/golang/protobuf v1.2.0 // indirect github.com/json-iterator/go v1.1.6 // indirect - github.com/mattn/go-isatty v0.0.4 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/pkg/errors v0.8.1 // indirect github.com/stretchr/testify v1.3.0 github.com/swaggo/swag v1.4.0 @@ -18,7 +14,5 @@ require ( golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc // indirect golang.org/x/tools v0.0.0-20190110015856-aa033095749b // indirect - gopkg.in/go-playground/assert.v1 v1.2.1 // indirect - gopkg.in/go-playground/validator.v8 v8.18.2 // indirect gopkg.in/yaml.v2 v2.2.2 // indirect ) diff --git a/go.sum b/go.sum index ca10315..549dac7 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ 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/gin-contrib/gzip v0.0.1 h1:ezvKOL6jH+jlzdHNE4h9h8q8uMpDQjyl0NN0Jd7jozc= +github.com/gin-contrib/gzip v0.0.1/go.mod h1:fGBJBCdt6qCZuCAOwWuFhBB4OOq9EFqlo5dEaFhhu5w= github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY= github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= @@ -21,6 +23,7 @@ github.com/go-openapi/swag v0.17.0 h1:iqrgMg7Q7SvtbWLlltPrkMs0UBJI6oTSs79JFRUi88 github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic= @@ -41,13 +44,17 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/swaggo/swag v1.4.0 h1:exX5ES4CdJWCCKmVPE+FAIN66cnHeMHU3i2SCMibBZc= github.com/swaggo/swag v1.4.0/go.mod h1:hog2WgeMOrQ/LvQ+o1YGTeT+vWVrbi0SiIslBtxKTyM= +github.com/ugorji/go/codec v0.0.0-20181022190402-e5e69e061d4f/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4= github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc h1:4gbWbmmPFp4ySWICouJl6emP0MyS31yy9SrTlAGFT+g= golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/swagger.go b/swagger.go index be02e55..445ecbc 100644 --- a/swagger.go +++ b/swagger.go @@ -4,6 +4,7 @@ import ( "html/template" "os" "regexp" + "strings" "golang.org/x/net/webdav" @@ -61,6 +62,16 @@ func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc { prefix := matches[1] h.Prefix = prefix + if strings.HasSuffix(path, ".html") { + c.Header("Content-Type", "text/html; charset=utf-8") + } else if strings.HasSuffix(path, ".css") { + c.Header("Content-Type", "text/css; charset=utf-8") + } else if strings.HasSuffix(path, ".js") { + c.Header("Content-Type", "application/javascript") + } else if strings.HasSuffix(path, ".json") { + c.Header("Content-Type", "application/json") + } + switch path { case "index.html": index.Execute(c.Writer, &swaggerUIBundle{ diff --git a/swagger_test.go b/swagger_test.go index 0a9ff18..5fcb40d 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -1,6 +1,7 @@ package ginSwagger import ( + "github.com/gin-contrib/gzip" "net/http/httptest" "os" "testing" @@ -9,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/swaggo/gin-swagger/swaggerFiles" - _ "github.com/swaggo/gin-swagger/example/docs" + _ "github.com/swaggo/gin-swagger/example/basic/docs" ) func TestWrapHandler(t *testing.T) { @@ -97,6 +98,31 @@ func TestDisablingCustomWrapHandler(t *testing.T) { assert.Equal(t, 404, w11.Code) } +func TestWithGzipMiddleware(t *testing.T) { + gin.SetMode(gin.TestMode) + router := gin.New() + + router.Use(gzip.Gzip(gzip.BestSpeed)) + + router.GET("/*any", WrapHandler(swaggerFiles.Handler)) + + w1 := performRequest("GET", "/index.html", router) + assert.Equal(t, 200, w1.Code) + assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8") + + w2 := performRequest("GET", "/swagger-ui.css", router) + assert.Equal(t, 200, w2.Code) + assert.Equal(t, w2.Header()["Content-Type"][0], "text/css; charset=utf-8") + + w3 := performRequest("GET", "/swagger-ui-bundle.js", router) + assert.Equal(t, 200, w3.Code) + assert.Equal(t, w3.Header()["Content-Type"][0], "application/javascript") + + w4 := performRequest("GET", "/doc.json", router) + assert.Equal(t, 200, w4.Code) + assert.Equal(t, w4.Header()["Content-Type"][0], "application/json") +} + func performRequest(method, target string, router *gin.Engine) *httptest.ResponseRecorder { r := httptest.NewRequest(method, target, nil) w := httptest.NewRecorder()