Skip to content

Commit 1ee3bc2

Browse files
committed
Added gzip middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 4f45cd1 commit 1ee3bc2

File tree

15 files changed

+138
-42
lines changed

15 files changed

+138
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func main() {
9191
e := echo.New()
9292

9393
// Middleware
94-
e.Use(mw.Logger)
94+
e.Use(mw.Logger())
9595

9696
// Routes
9797
e.Get("/", hello)

context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (c *Context) Param(name string) (value string) {
5353

5454
// Bind binds the request body into specified type v. Default binder does it
5555
// based on Content-Type header.
56-
func (c *Context) Bind(v interface{}) *HTTPError {
57-
return c.echo.binder(c.Request, v)
56+
func (c *Context) Bind(i interface{}) *HTTPError {
57+
return c.echo.binder(c.Request, i)
5858
}
5959

6060
// Render invokes the registered HTML template renderer and sends a text/html
@@ -69,10 +69,10 @@ func (c *Context) Render(code int, name string, data interface{}) *HTTPError {
6969
}
7070

7171
// JSON sends an application/json response with status code.
72-
func (c *Context) JSON(code int, v interface{}) *HTTPError {
72+
func (c *Context) JSON(code int, i interface{}) *HTTPError {
7373
c.Response.Header().Set(ContentType, ApplicationJSON+"; charset=utf-8")
7474
c.Response.WriteHeader(code)
75-
if err := json.NewEncoder(c.Response).Encode(v); err != nil {
75+
if err := json.NewEncoder(c.Response).Encode(i); err != nil {
7676
return &HTTPError{Error: err}
7777
}
7878
return nil

echo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ const (
9090
//---------
9191

9292
Accept = "Accept"
93+
AcceptEncoding = "Accept-Encoding"
9394
ContentDisposition = "Content-Disposition"
95+
ContentEncoding = "Content-Encoding"
9496
ContentLength = "Content-Length"
9597
ContentType = "Content-Type"
9698
Authorization = "Authorization"

examples/crud/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
e := echo.New()
6262

6363
// Middleware
64-
e.Use(mw.Logger)
64+
e.Use(mw.Logger())
6565

6666
// Routes
6767
e.Post("/users", createUser)

examples/hello/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
e := echo.New()
1818

1919
// Middleware
20-
e.Use(mw.Logger)
20+
e.Use(mw.Logger())
2121

2222
// Routes
2323
e.Get("/", hello)

examples/middleware/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
//------------
2222

2323
// Logger
24-
e.Use(mw.Logger)
24+
e.Use(mw.Logger())
2525

2626
// Basic auth
2727
e.Use(mw.BasicAuth(func(u, p string) bool {
@@ -41,6 +41,9 @@ func main() {
4141

4242
// e.Use(mw.RedirectToSlash())
4343

44+
// Gzip
45+
e.Use(mw.Gzip())
46+
4447
// Routes
4548
e.Get("/", hello)
4649

examples/web/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func main() {
6565
e := echo.New()
6666

6767
// Middleware
68-
e.Use(mw.Logger)
68+
e.Use(mw.Logger())
6969

7070
//------------------------
7171
// Third-party middleware

middleware/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
Basic = "Basic"
1515
)
1616

17-
// BasicAuth provides HTTP basic authentication middleware.
17+
// BasicAuth provides HTTP basic authentication.
1818
func BasicAuth(fn AuthFunc) echo.HandlerFunc {
1919
return func(c *echo.Context) (he *echo.HTTPError) {
2020
auth := c.Request.Header.Get(echo.Authorization)

middleware/compress.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package middleware
2+
3+
import (
4+
"compress/gzip"
5+
"io"
6+
"strings"
7+
8+
"github.com/labstack/echo"
9+
)
10+
11+
type (
12+
gzipResponseWriter struct {
13+
io.Writer
14+
*echo.Response
15+
}
16+
)
17+
18+
func (g gzipResponseWriter) Write(b []byte) (int, error) {
19+
return g.Writer.Write(b)
20+
}
21+
22+
// Gzip compresses HTTP response using gzip compression scheme.
23+
func Gzip() echo.MiddlewareFunc {
24+
scheme := "gzip"
25+
26+
return func(h echo.HandlerFunc) echo.HandlerFunc {
27+
return func(c *echo.Context) *echo.HTTPError {
28+
if !strings.Contains(c.Request.Header.Get(echo.AcceptEncoding), scheme) {
29+
return nil
30+
}
31+
32+
w := gzip.NewWriter(c.Response.Writer)
33+
defer w.Close()
34+
gw := gzipResponseWriter{Writer: w, Response: c.Response}
35+
c.Response.Header().Set(echo.ContentEncoding, scheme)
36+
c.Response = &echo.Response{Writer: gw}
37+
if he := h(c); he != nil {
38+
c.Error(he)
39+
}
40+
return nil
41+
}
42+
}
43+
}

middleware/compress_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"compress/gzip"
9+
"github.com/labstack/echo"
10+
"io/ioutil"
11+
)
12+
13+
func TestGzip(t *testing.T) {
14+
req, _ := http.NewRequest(echo.GET, "/", nil)
15+
req.Header.Set(echo.AcceptEncoding, "gzip")
16+
w := httptest.NewRecorder()
17+
res := &echo.Response{Writer: w}
18+
c := echo.NewContext(req, res, echo.New())
19+
Gzip()(func(c *echo.Context) *echo.HTTPError {
20+
return c.String(http.StatusOK, "test")
21+
})(c)
22+
23+
if w.Header().Get(echo.ContentEncoding) != "gzip" {
24+
t.Errorf("expected Content-Encoding: gzip, got %d", w.Header().Get(echo.ContentEncoding))
25+
}
26+
27+
r, err := gzip.NewReader(w.Body)
28+
defer r.Close()
29+
if err != nil {
30+
t.Error(err)
31+
}
32+
33+
b, err := ioutil.ReadAll(r)
34+
if err != nil {
35+
t.Error(err)
36+
}
37+
s := string(b)
38+
39+
if s != "test" {
40+
t.Errorf(`expected "test", got "%s"`, s)
41+
}
42+
}

0 commit comments

Comments
 (0)