Skip to content

Commit

Permalink
feat: support gzip decode (songquanpeng#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion authored Dec 4, 2024
1 parent 6ab87f8 commit 7c8628b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions middleware/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package middleware

import (
"compress/gzip"
"github.com/gin-gonic/gin"
"io"
"net/http"
)

func GzipDecodeMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if c.GetHeader("Content-Encoding") == "gzip" {
gzipReader, err := gzip.NewReader(c.Request.Body)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
defer gzipReader.Close()

// Replace the request body with the decompressed data
c.Request.Body = io.NopCloser(gzipReader)
}

// Continue processing the request
c.Next()
}
}
1 change: 1 addition & 0 deletions router/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func SetRelayRouter(router *gin.Engine) {
router.Use(middleware.CORS())
router.Use(middleware.GzipDecodeMiddleware())
// https://platform.openai.com/docs/api-reference/introduction
modelsRouter := router.Group("/v1/models")
modelsRouter.Use(middleware.TokenAuth())
Expand Down

0 comments on commit 7c8628b

Please sign in to comment.