Skip to content

Commit

Permalink
chore: fix lint
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Oct 2, 2021
1 parent 007a8f4 commit 51dd498
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 61 deletions.
115 changes: 61 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GZIP gin's middleware

[![Build Status](https://travis-ci.org/gin-contrib/gzip.svg)](https://travis-ci.org/gin-contrib/gzip)
[![Run Tests](https://github.com/gin-contrib/gzip/actions/workflows/go.yml/badge.svg)](https://github.com/gin-contrib/gzip/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/gin-contrib/gzip/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/gzip)
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/gzip)](https://goreportcard.com/report/github.com/gin-contrib/gzip)
[![GoDoc](https://godoc.org/github.com/gin-contrib/gzip?status.svg)](https://godoc.org/github.com/gin-contrib/gzip)
Expand Down Expand Up @@ -28,23 +28,25 @@ Canonical example:
package main

import (
"fmt"
"net/http"
"time"
"fmt"
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```

Expand All @@ -54,23 +56,25 @@ Customized Excluded Extensions
package main

import (
"fmt"
"net/http"
"time"
"fmt"
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```

Expand All @@ -80,49 +84,52 @@ Customized Excluded Paths
package main

import (
"fmt"
"net/http"
"time"
"fmt"
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```


Customized Excluded Paths

```go
package main

import (
"fmt"
"net/http"
"time"
"fmt"
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
r := gin.Default()
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"})))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```
5 changes: 4 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"net/http"
"time"

Expand All @@ -17,5 +18,7 @@ func main() {
})

// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
4 changes: 0 additions & 4 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func newCloseNotifyingRecorder() *closeNotifyingRecorder {
}
}

func (c *closeNotifyingRecorder) close() {
c.closed <- true
}

func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
return c.closed
}
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e ExcludedPaths) Contains(requestURI string) bool {
type ExcludedPathesRegexs []*regexp.Regexp

func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
result := make([]*regexp.Regexp, len(regexs), len(regexs))
result := make([]*regexp.Regexp, len(regexs))
for i, reg := range regexs {
result[i] = regexp.MustCompile(reg)
}
Expand All @@ -107,7 +107,7 @@ func DefaultDecompressHandle(c *gin.Context) {
}
r, err := gzip.NewReader(c.Request.Body)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Request.Header.Del("Content-Encoding")
Expand Down

0 comments on commit 51dd498

Please sign in to comment.