Files
gin-contrib/gzip/README.md
T
2024-04-02 15:13:13 +03:00

120 lines
2.1 KiB
Markdown

# GZIP gin's middleware
Gin middleware to enable `GZIP` support.
Copied from [gin-contrib/gzip](https://github.com/gin-contrib/gzip)
## Usage
Canonical example:
```go
package main
import (
"fmt"
"net/http"
"time"
"git.company.lan/gopkg/gin-contrib/gzip"
"git.company.lan/gopkg/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 main
import (
"fmt"
"net/http"
"time"
"git.company.lan/gopkg/gin-contrib/gzip"
"git.company.lan/gopkg/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 main
import (
"fmt"
"net/http"
"time"
"git.company.lan/gopkg/gin-contrib/gzip"
"git.company.lan/gopkg/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 main
import (
"fmt"
"net/http"
"time"
"git.company.lan/gopkg/gin-contrib/gzip"
"git.company.lan/gopkg/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)
}
}
```