mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
dev day #79
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SmartCompression applies gzip compression intelligently
|
||||
// Skips compression for already compressed formats and small responses
|
||||
func SmartCompression() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Skip compression for already compressed formats
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
if shouldSkipCompression(contentType) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// Skip compression for small responses (< 1KB overhead not worth it)
|
||||
// This is handled by checking response size in writer
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// shouldSkipCompression checks if content type should skip compression
|
||||
func shouldSkipCompression(contentType string) bool {
|
||||
// Already compressed formats
|
||||
skipTypes := []string{
|
||||
"image/jpeg",
|
||||
"image/jpg",
|
||||
"image/png",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"video/",
|
||||
"audio/",
|
||||
"application/zip",
|
||||
"application/x-zip",
|
||||
"application/x-gzip",
|
||||
"application/gzip",
|
||||
"application/x-compress",
|
||||
"application/pdf", // PDFs are already compressed
|
||||
}
|
||||
|
||||
for _, skip := range skipTypes {
|
||||
if strings.Contains(strings.ToLower(contentType), skip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user