README.md (2834B)
1 # GZIP gin's middleware 2 3 [](https://github.com/gin-contrib/gzip/actions/workflows/go.yml) 4 [](https://codecov.io/gh/gin-contrib/gzip) 5 [](https://goreportcard.com/report/github.com/gin-contrib/gzip) 6 [](https://godoc.org/github.com/gin-contrib/gzip) 7 [](https://gitter.im/gin-gonic/gin) 8 9 Gin middleware to enable `GZIP` support. 10 11 ## Usage 12 13 Download and install it: 14 15 ```sh 16 go get github.com/gin-contrib/gzip 17 ``` 18 19 Import it in your code: 20 21 ```go 22 import "github.com/gin-contrib/gzip" 23 ``` 24 25 Canonical example: 26 27 ```go 28 package main 29 30 import ( 31 "fmt" 32 "net/http" 33 "time" 34 35 "github.com/gin-contrib/gzip" 36 "github.com/gin-gonic/gin" 37 ) 38 39 func main() { 40 r := gin.Default() 41 r.Use(gzip.Gzip(gzip.DefaultCompression)) 42 r.GET("/ping", func(c *gin.Context) { 43 c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) 44 }) 45 46 // Listen and Server in 0.0.0.0:8080 47 if err := r.Run(":8080"); err != nil { 48 log.Fatal(err) 49 } 50 } 51 ``` 52 53 Customized Excluded Extensions 54 55 ```go 56 package main 57 58 import ( 59 "fmt" 60 "net/http" 61 "time" 62 63 "github.com/gin-contrib/gzip" 64 "github.com/gin-gonic/gin" 65 ) 66 67 func main() { 68 r := gin.Default() 69 r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"}))) 70 r.GET("/ping", func(c *gin.Context) { 71 c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) 72 }) 73 74 // Listen and Server in 0.0.0.0:8080 75 if err := r.Run(":8080"); err != nil { 76 log.Fatal(err) 77 } 78 } 79 ``` 80 81 Customized Excluded Paths 82 83 ```go 84 package main 85 86 import ( 87 "fmt" 88 "net/http" 89 "time" 90 91 "github.com/gin-contrib/gzip" 92 "github.com/gin-gonic/gin" 93 ) 94 95 func main() { 96 r := gin.Default() 97 r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"}))) 98 r.GET("/ping", func(c *gin.Context) { 99 c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) 100 }) 101 102 // Listen and Server in 0.0.0.0:8080 103 if err := r.Run(":8080"); err != nil { 104 log.Fatal(err) 105 } 106 } 107 ``` 108 109 Customized Excluded Paths 110 111 ```go 112 package main 113 114 import ( 115 "fmt" 116 "net/http" 117 "time" 118 119 "github.com/gin-contrib/gzip" 120 "github.com/gin-gonic/gin" 121 ) 122 123 func main() { 124 r := gin.Default() 125 r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"}))) 126 r.GET("/ping", func(c *gin.Context) { 127 c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) 128 }) 129 130 // Listen and Server in 0.0.0.0:8080 131 if err := r.Run(":8080"); err != nil { 132 log.Fatal(err) 133 } 134 } 135 ```