Skip to content

Commit

Permalink
RHINENG-11685: gzip middleware with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Aug 28, 2024
1 parent e628dc7 commit 21087ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 1 addition & 2 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"app/manager/middlewares"
"app/manager/routes"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func RunManager() {
app.Use(middlewares.MaxConnections(utils.CoreCfg.MaxGinConnections))
app.Use(middlewares.Ratelimit(utils.CoreCfg.Ratelimit))
app.Use(middlewares.RequestResponseLogger())
app.Use(gzip.Gzip(gzip.DefaultCompression))
app.Use(middlewares.Gzip())
endpointsConfig := getEndpointsConfig()
middlewares.SetSwagger(app, endpointsConfig)
app.Use(middlewares.WithTimeout(utils.CoreCfg.ResponseTimeout))
Expand Down
15 changes: 15 additions & 0 deletions manager/middlewares/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package middlewares

import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func Gzip(options ...gzip.Option) gin.HandlerFunc {
gzipFn := gzip.Gzip(gzip.DefaultCompression, options...)
return func(c *gin.Context) {
tempLogDebugGinContextRequestHeader(c, "Gzip before")
defer tempLogDebugGinContextRequestHeader(c, "Gzip after")
gzipFn(c)
}
}
11 changes: 5 additions & 6 deletions manager/middlewares/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"app/manager/config"
"fmt"
"net/http"
"net/http/httputil"
"strconv"
"strings"

Expand Down Expand Up @@ -204,12 +205,10 @@ func RBAC() gin.HandlerFunc {

func tempLogDebugGinContextRequestHeader(c *gin.Context, origin string) {
if c != nil {
if c.Request != nil {
utils.LogDebug("origin", origin, "gin_context_req_header", c.Request.Header, "gin context request handler")
} else {
utils.LogDebug("origin", origin, "gin_context_req", nil, "gin context request handler")
dump, err := httputil.DumpRequest(c.Request, false)
if err != nil {
utils.LogDebug("origin", origin, "err", err.Error(), "gin_context_req", nil, "gin context request handler")
}
} else {
utils.LogDebug("origin", origin, "gin_context", nil, "gin context request handler")
utils.LogDebug("origin", origin, "gin_context_req", string(dump), "gin context request handler")
}
}

0 comments on commit 21087ae

Please sign in to comment.