You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by bz...@apache.org on 2021/10/20 17:48:13 UTC

[apisix-dashboard] branch master updated: feat: add gzip middleware (#2178)

This is an automated email from the ASF dual-hosted git repository.

bzp2010 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 05e10ff  feat: add gzip middleware (#2178)
05e10ff is described below

commit 05e10ffda9dda2e6cbf79af3809b65b50c4ec689
Author: Nic <pr...@gmail.com>
AuthorDate: Thu Oct 21 01:48:04 2021 +0800

    feat: add gzip middleware (#2178)
    
    Co-authored-by: bzp2010 <bz...@apache.org>
---
 api/go.mod                              |  1 +
 api/go.sum                              |  2 ++
 api/internal/route.go                   |  2 ++
 api/test/e2enew/gzip/gzip_suite_test.go | 29 ++++++++++++++++++++++++++
 api/test/e2enew/gzip/gzip_test.go       | 37 +++++++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+)

diff --git a/api/go.mod b/api/go.mod
index 003f6ed..04fc27b 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 80d1561..fefb389 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 6a507cc..574e113 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 0000000..13380cf
--- /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 0000000..5f764ec
--- /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"},
+		})
+	})
+})