769e76313f
- Recovery middleware (no more crash-on-panic), request metrics/monitoring store + admin API, in-memory TTL caching for read-heavy option/question/ category lookups, gzip + rate-limiting middleware, sanitized chat message encoding, and pg_dump-based database backups with an admin API. - Fix N+1 queries in the discover feed, candidate detail, and match list endpoints by batching selection/photo/user/last-message lookups into single queries instead of one-per-item loops. - Tune DB connection pooling and add HTTP server timeouts + graceful shutdown so the API degrades gracefully instead of falling over under load. - Fix a null (vs empty array) JSON response for candidates/matches without photos or interests, which crashed the web app's swipe card.
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type gzipWriter struct {
|
|
gin.ResponseWriter
|
|
writer io.Writer
|
|
}
|
|
|
|
func (g *gzipWriter) Write(data []byte) (int, error) {
|
|
return g.writer.Write(data)
|
|
}
|
|
|
|
func (g *gzipWriter) WriteString(s string) (int, error) {
|
|
return g.writer.Write([]byte(s))
|
|
}
|
|
|
|
// Gzip پاسخهای JSON رو (وقتی کلاینت gzip رو توی Accept-Encoding پشتیبانی کنه)
|
|
// فشرده میکنه؛ بدون هیچ وابستگی بیرونی، فقط با compress/gzip استاندارد گو.
|
|
// برای مسیرهایی مثل فید دیسکاوری که پاسخ JSON نسبتاً بزرگی برمیگردونن، حجم
|
|
// انتقال رو محسوس کم میکنه.
|
|
func Gzip() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if !strings.Contains(c.GetHeader("Accept-Encoding"), "gzip") {
|
|
c.Next()
|
|
return
|
|
}
|
|
// فایلهای عکسِ آپلودشده از قبل فشردهان (jpeg/png/webp)؛ دوباره gzip
|
|
// کردنشون فقط CPU هدر میده بدون فایدهی محسوس.
|
|
if strings.HasPrefix(c.Request.URL.Path, "/uploads") {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
gz := gzip.NewWriter(c.Writer)
|
|
defer gz.Close()
|
|
|
|
c.Header("Content-Encoding", "gzip")
|
|
c.Header("Vary", "Accept-Encoding")
|
|
c.Writer = &gzipWriter{ResponseWriter: c.Writer, writer: gz}
|
|
c.Next()
|
|
}
|
|
}
|