Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gin-contrib/gzip
Gin middleware to enable GZIP support.
https://github.com/gin-contrib/gzip
Last synced: about 2 months ago
JSON representation
Gin middleware to enable GZIP support.
- Host: GitHub
- URL: https://github.com/gin-contrib/gzip
- Owner: gin-contrib
- License: mit
- Created: 2015-11-11T22:10:32.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-05-02T01:23:28.000Z (9 months ago)
- Last Synced: 2024-05-02T17:20:26.247Z (9 months ago)
- Language: Go
- Homepage:
- Size: 93.8 KB
- Stars: 315
- Watchers: 9
- Forks: 77
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gin - gin-contrib/gzip
README
# GZIP gin's middleware
[![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)Gin middleware to enable `GZIP` support.
## Usage
Download and install it:
```sh
go get github.com/gin-contrib/gzip
```Import it in your code:
```go
import "github.com/gin-contrib/gzip"
```Canonical example:
```go
package mainimport (
"fmt"
"net/http"
"time""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
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```Customized Excluded Extensions
```go
package mainimport (
"fmt"
"net/http"
"time""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
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```Customized Excluded Paths
```go
package mainimport (
"fmt"
"net/http"
"time""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
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```Customized Excluded Paths
```go
package mainimport (
"fmt"
"net/http"
"time""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
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```