diff --git a/api/go.mod b/api/go.mod index 003f6ed7c5..04fc27bdae 100644 --- a/api/go.mod +++ b/api/go.mod @@ -15,6 +15,7 @@ require ( github.com/dustin/go-humanize v1.0.0 // indirect github.com/evanphx/json-patch/v5 v5.1.0 github.com/getkin/kin-openapi v0.33.0 + github.com/gin-contrib/gzip v0.0.3 github.com/gin-contrib/pprof v1.3.0 github.com/gin-contrib/static v0.0.0-20200916080430-d45d9a37d28e github.com/gin-gonic/gin v1.6.3 diff --git a/api/go.sum b/api/go.sum index 80d156151d..fefb389a93 100644 --- a/api/go.sum +++ b/api/go.sum @@ -118,6 +118,8 @@ github.com/getkin/kin-openapi v0.33.0 h1:KccukV3/1h95R0OP7vfWB3KVy9lxA5i8i3BwlA3 github.com/getkin/kin-openapi v0.33.0/go.mod h1:ZJSfy1PxJv2QQvH9EdBj3nupRTVvV42mkW6zKUlRBwk= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gin-contrib/gzip v0.0.3 h1:etUaeesHhEORpZMp18zoOhepboiWnFtXrBZxszWUn4k= +github.com/gin-contrib/gzip v0.0.3/go.mod h1:YxxswVZIqOvcHEQpsSn+QF5guQtO1dCfy0shBPy4jFc= github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0= github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= diff --git a/api/internal/route.go b/api/internal/route.go index 6a507cc258..574e113bb3 100644 --- a/api/internal/route.go +++ b/api/internal/route.go @@ -44,6 +44,7 @@ import ( "github.com/apisix/manager-api/internal/handler/tool" "github.com/apisix/manager-api/internal/handler/upstream" "github.com/apisix/manager-api/internal/log" + "github.com/gin-contrib/gzip" ) func SetUpRouter() *gin.Engine { @@ -55,6 +56,7 @@ func SetUpRouter() *gin.Engine { r := gin.New() logger := log.GetLogger(log.AccessLog) r.Use(filter.CORS(), filter.RequestId(), filter.IPFilter(), filter.RequestLogHandler(logger), filter.SchemaCheck(), filter.RecoverHandler()) + r.Use(gzip.Gzip(gzip.DefaultCompression)) r.Use(static.Serve("/", static.LocalFile(filepath.Join(conf.WorkDir, conf.WebDir), false))) r.NoRoute(func(c *gin.Context) { c.File(fmt.Sprintf("%s/index.html", filepath.Join(conf.WorkDir, conf.WebDir))) diff --git a/api/test/e2enew/gzip/gzip_suite_test.go b/api/test/e2enew/gzip/gzip_suite_test.go new file mode 100644 index 0000000000..13380cf065 --- /dev/null +++ b/api/test/e2enew/gzip/gzip_suite_test.go @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gzip_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestGzip(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "gzip suite") +} diff --git a/api/test/e2enew/gzip/gzip_test.go b/api/test/e2enew/gzip/gzip_test.go new file mode 100644 index 0000000000..5f764ec8dc --- /dev/null +++ b/api/test/e2enew/gzip/gzip_test.go @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gzip + +import ( + "net/http" + + "github.com/onsi/ginkgo" + + "github.com/apisix/manager-api/test/e2enew/base" +) + +var _ = ginkgo.Describe("Gzip enable", func() { + ginkgo.It("get index.html", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/", + Headers: map[string]string{"Accept-Encoding": "gzip, deflate, br"}, + ExpectHeaders: map[string]string{"Content-Encoding": "gzip"}, + }) + }) +})