initial commit

This commit is contained in:
m.zare
2026-04-10 18:25:21 +03:30
commit 77ca6c34a3
263 changed files with 34470 additions and 0 deletions

113
pkg/health/infra_checker.go Normal file
View File

@@ -0,0 +1,113 @@
package health
import (
"context"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
rabbitmq "base/pkg/rabbit"
"time"
)
func DatabaseHealthChecker(db *gorm.DB) Checker {
return func(ctx context.Context) HealthCheck {
start := time.Now()
check := HealthCheck{
Name: "database",
Timestamp: time.Now(),
}
// Perform health check
sqlDB, err := db.DB()
if err != nil {
check.Status = StatusUnhealthy
check.Message = "Failed to get database connection: " + err.Error()
check.Duration = time.Since(start)
return check
}
err = sqlDB.PingContext(ctx)
if err != nil {
check.Status = StatusUnhealthy
check.Message = "Database ping failed: " + err.Error()
check.Duration = time.Since(start)
return check
}
check.Status = StatusHealthy
check.Message = "Database connection is healthy"
check.Duration = time.Since(start)
check.Details = map[string]interface{}{
"connected": true,
}
return check
}
}
func RabbitMQHealthChecker(rabbitmq rabbitmq.Client) Checker {
return func(ctx context.Context) HealthCheck {
start := time.Now()
check := HealthCheck{
Name: "rabbitmq",
Timestamp: time.Now(),
}
// Perform health check
err := rabbitmq.HealthCheck()
if err != nil {
check.Status = StatusUnhealthy
check.Message = "RabbitMQ health check failed: " + err.Error()
check.Duration = time.Since(start)
return check
}
check.Status = StatusHealthy
check.Message = "RabbitMQ connection is healthy"
check.Duration = time.Since(start)
check.Details = map[string]interface{}{
"connected": true,
}
return check
}
}
func RedisHealthChecker(redis *redis.Client) Checker {
return func(ctx context.Context) HealthCheck {
start := time.Now()
check := HealthCheck{
Name: "redis",
Timestamp: time.Now(),
}
// Perform health check
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_, err := redis.Ping(ctx).Result()
if err != nil {
check.Status = StatusUnhealthy
check.Message = "Redis ping failed: " + err.Error()
check.Duration = time.Since(start)
return check
}
// Get Redis info
info, err := redis.Info(ctx, "server", "clients", "memory", "stats").Result()
if err != nil {
check.Status = StatusDegraded
check.Message = "Redis is responding but info command failed: " + err.Error()
check.Duration = time.Since(start)
return check
}
check.Status = StatusHealthy
check.Message = "Redis connection is healthy"
check.Duration = time.Since(start)
check.Details = map[string]interface{}{
"info": info,
}
return check
}
}