Add production hardening: monitoring, backups, caching, and high-traffic resilience
- 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.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalcAge_NilBirthDate(t *testing.T) {
|
||||
if age := CalcAge(nil); age != 0 {
|
||||
t.Fatalf("expected 0 for nil birth date, got %d", age)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcAge_BirthdayAlreadyPassedThisYear(t *testing.T) {
|
||||
birth := time.Now().AddDate(-25, -1, 0) // ۲۵ سال و ۱ ماه پیش -> تولد امسال رد شده
|
||||
if age := CalcAge(&birth); age != 25 {
|
||||
t.Fatalf("expected age 25, got %d", age)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalcAge_BirthdayNotYetThisYear(t *testing.T) {
|
||||
birth := time.Now().AddDate(-25, 1, 0) // تولد ۱ ماه دیگهست -> هنوز ۲۴ سالشه
|
||||
if age := CalcAge(&birth); age != 24 {
|
||||
t.Fatalf("expected age 24, got %d", age)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHaversineKM_SamePoint(t *testing.T) {
|
||||
d := HaversineKM(35.6892, 51.3890, 35.6892, 51.3890)
|
||||
if d > 0.001 {
|
||||
t.Fatalf("expected ~0km for identical points, got %v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHaversineKM_TehranToIsfahan(t *testing.T) {
|
||||
// فاصلهی تهران تا اصفهان تقریباً ۳۴۰ کیلومتره (خط مستقیم)
|
||||
d := HaversineKM(35.6892, 51.3890, 32.6546, 51.6680)
|
||||
if d < 300 || d > 380 {
|
||||
t.Fatalf("expected ~340km between Tehran and Isfahan, got %v", d)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidMessageEncoding = errors.New("متن پیام معتبر نیست (کدگذاری اشتباه)")
|
||||
ErrMessageEmpty = errors.New("متن پیام نمیتواند خالی باشد")
|
||||
ErrMessageTooLong = errors.New("متن پیام بیش از حد مجاز طولانی است")
|
||||
)
|
||||
|
||||
const MaxMessageLength = 2000
|
||||
|
||||
// SanitizeMessageText متن یک پیام چت رو قبل از ذخیرهسازی پاکسازی/اعتبارسنجی میکنه:
|
||||
// - رد کردن رشتههایی که UTF-8 معتبر نیستن (بایت خراب/دستکاریشده)
|
||||
// - حذف کاراکترهای کنترلی (بهجز \n و \t) که میتونن نمایش یا لاگها رو خراب کنن
|
||||
// - trim فاصلههای اضافه و محدود کردن طول
|
||||
func SanitizeMessageText(raw string) (string, error) {
|
||||
if !utf8.ValidString(raw) {
|
||||
return "", ErrInvalidMessageEncoding
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
b.Grow(len(raw))
|
||||
for _, r := range raw {
|
||||
if r == '\n' || r == '\t' {
|
||||
b.WriteRune(r)
|
||||
continue
|
||||
}
|
||||
if r < 0x20 || r == 0x7F {
|
||||
continue
|
||||
}
|
||||
b.WriteRune(r)
|
||||
}
|
||||
|
||||
text := strings.TrimSpace(b.String())
|
||||
if text == "" {
|
||||
return "", ErrMessageEmpty
|
||||
}
|
||||
if utf8.RuneCountInString(text) > MaxMessageLength {
|
||||
return "", ErrMessageTooLong
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSanitizeMessageText_TrimsWhitespace(t *testing.T) {
|
||||
got, err := SanitizeMessageText(" سلام دوست من ")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "سلام دوست من" {
|
||||
t.Fatalf("expected trimmed text, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_EmptyAfterTrim(t *testing.T) {
|
||||
_, err := SanitizeMessageText(" ")
|
||||
if !errors.Is(err, ErrMessageEmpty) {
|
||||
t.Fatalf("expected ErrMessageEmpty, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_StripsControlChars(t *testing.T) {
|
||||
got, err := SanitizeMessageText("سلام\x00\x07دنیا")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "سلامدنیا" {
|
||||
t.Fatalf("expected control chars stripped, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_KeepsNewlinesAndTabs(t *testing.T) {
|
||||
got, err := SanitizeMessageText("خط اول\nخط دوم\tبا تب")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "خط اول\nخط دوم\tبا تب" {
|
||||
t.Fatalf("expected newline/tab preserved, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_InvalidUTF8(t *testing.T) {
|
||||
invalid := string([]byte{0xff, 0xfe, 0xfd})
|
||||
_, err := SanitizeMessageText(invalid)
|
||||
if !errors.Is(err, ErrInvalidMessageEncoding) {
|
||||
t.Fatalf("expected ErrInvalidMessageEncoding, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_TooLong(t *testing.T) {
|
||||
long := strings.Repeat("a", MaxMessageLength+1)
|
||||
_, err := SanitizeMessageText(long)
|
||||
if !errors.Is(err, ErrMessageTooLong) {
|
||||
t.Fatalf("expected ErrMessageTooLong, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_ExactlyMaxLength(t *testing.T) {
|
||||
exact := strings.Repeat("a", MaxMessageLength)
|
||||
got, err := SanitizeMessageText(exact)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error at exact max length: %v", err)
|
||||
}
|
||||
if len(got) != MaxMessageLength {
|
||||
t.Fatalf("expected length %d, got %d", MaxMessageLength, len(got))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user