package service import ( "context" "time" "github.com/redis/go-redis/v9" "gorm.io/gorm" ) type HealthCheckService struct { db *gorm.DB rdb *redis.Client } func NewHealthCheckService(db *gorm.DB, rdb *redis.Client) *HealthCheckService { return &HealthCheckService{ db: db, rdb: rdb, } } type HealthStatus struct { Status string `json:"status"` Database string `json:"database"` Redis string `json:"redis"` Uptime string `json:"uptime"` } var startTime = time.Now() func (s *HealthCheckService) Check(ctx context.Context) HealthStatus { status := HealthStatus{ Status: "ok", Database: "up", Redis: "up", Uptime: time.Since(startTime).String(), } sqlDB, err := s.db.DB() if err != nil || sqlDB.Ping() != nil { status.Database = "down" status.Status = "degraded" } if s.rdb.Ping(ctx).Err() != nil { status.Redis = "down" status.Status = "degraded" } if status.Database == "down" && status.Redis == "down" { status.Status = "down" } return status }