# README
GZIP gin's middleware
Gin middleware to enable GZIP
support.
Usage
Download and install it:
go get github.com/gin-contrib/gzip
Import it in your code:
import "github.com/gin-contrib/gzip"
Canonical example:
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
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
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 with Regex
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)
}
}
Server Push
package main
import (
"fmt"
"log"
"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("/stream", func(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Connection", "keep-alive")
for i := 0; i < 10; i++ {
fmt.Fprintf(c.Writer, "id: %d\ndata: tick %d\n\n", i, time.Now().Unix())
c.Writer.Flush()
time.Sleep(1 * time.Second)
}
})
// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
# Functions
DefaultDecompressHandle is a middleware function for the Gin framework that decompresses the request body if it is gzip encoded.
No description provided by the author
NewExcludedExtensions creates a new ExcludedExtensions map from a slice of file extensions.
NewExcludedPathesRegexs creates a new ExcludedPathesRegexs slice from a slice of regex patterns.
NewExcludedPaths creates a new ExcludedPaths slice from a slice of paths.
WithCustomShouldCompressFn returns an Option that sets the CustomShouldCompressFn field of the Options struct.
WithDecompressFn returns an Option that sets the DecompressFn field of the Options struct.
WithDecompressOnly is an option that configures the gzip middleware to only decompress incoming requests without compressing the responses.
WithExcludedExtensions returns an Option that sets the ExcludedExtensions field of the Options struct.
WithExcludedPaths returns an Option that sets the ExcludedPaths field of the Options struct.
WithExcludedPathsRegexs returns an Option that sets the ExcludedPathesRegexs field of the Options struct.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
DefaultExcludedExtentions is a predefined list of file extensions that should be excluded from gzip compression.
ErrUnsupportedContentEncoding is an error that indicates the content encoding is not supported by the application.
# Interfaces
Option is an interface that defines a method to apply a configuration to a given config instance.
# Type aliases
Using map for better lookup performance.
No description provided by the author
No description provided by the author