mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-24 20:56:39 +00:00
a089a98ea9
* add gin gzip dependency * add gzip middleware to router * go mod tidy
136 lines
2.8 KiB
Markdown
136 lines
2.8 KiB
Markdown
# 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)
|
|
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin)
|
|
|
|
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 main
|
|
|
|
import (
|
|
"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 main
|
|
|
|
import (
|
|
"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 main
|
|
|
|
import (
|
|
"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 main
|
|
|
|
import (
|
|
"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)
|
|
}
|
|
}
|
|
```
|