feat: gzip middleware.

This commit is contained in:
2025-03-19 16:58:09 +08:00
parent c0a5834ab4
commit 1d9b568bef
2 changed files with 24 additions and 0 deletions

22
middleware/gzip.go Normal file
View File

@ -0,0 +1,22 @@
package middleware
import (
"compress/gzip"
"github.com/gin-gonic/gin"
"net/http"
)
func GzipMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Header.Get("Content-Encoding") == "gzip" {
gzReader, err := gzip.NewReader(c.Request.Body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid gzip format"})
return
}
defer gzReader.Close()
c.Request.Body = gzReader
}
c.Next()
}
}