2026-07-10 12:54:45 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-07-15 15:23:59 +00:00
|
|
|
"net/http"
|
|
|
|
|
"time"
|
2026-07-10 12:54:45 +00:00
|
|
|
|
2026-07-15 15:23:59 +00:00
|
|
|
"apps/backend/internal/response"
|
2026-07-10 12:54:45 +00:00
|
|
|
"apps/backend/internal/svc"
|
|
|
|
|
"apps/backend/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PingLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
|
|
|
|
|
return &PingLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *PingLogic) Ping() (resp *types.HealthData, err error) {
|
2026-07-15 15:23:59 +00:00
|
|
|
if err := redisHealth(l.ctx, l.svcCtx); err != nil {
|
|
|
|
|
l.Errorf("ping redis health failed: %v", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-07-10 12:54:45 +00:00
|
|
|
return &types.HealthData{Status: "ok", Service: "haixun-gateway"}, nil
|
|
|
|
|
}
|
2026-07-15 15:23:59 +00:00
|
|
|
|
|
|
|
|
func redisHealth(ctx context.Context, svcCtx *svc.ServiceContext) error {
|
|
|
|
|
if svcCtx == nil || svcCtx.Redis == nil {
|
|
|
|
|
return response.Biz(http.StatusServiceUnavailable, 503001, "redis unavailable")
|
|
|
|
|
}
|
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
if !svcCtx.Redis.PingCtx(pingCtx) {
|
|
|
|
|
return response.Biz(http.StatusServiceUnavailable, 503001, "redis unavailable")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|