1 Star 1 Fork 0

kade/library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
health.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
kade 提交于 2024-03-25 11:39 . 第三方库,初步完成
package gin
import (
"gitee.com/go-kade/library/ioc/config/http"
"gitee.com/go-kade/library/ioc"
ioc_health "gitee.com/go-kade/library/ioc/apps/health"
"gitee.com/go-kade/library/ioc/config/log"
"github.com/gin-gonic/gin"
h_response "gitee.com/go-kade/library/http/response"
"github.com/rs/zerolog"
"google.golang.org/grpc/health"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
)
func init() {
ioc.Api().Registry(&HealthChecker{
HealthCheck: ioc_health.HealthCheck{
Path: ioc_health.DEFAUL_HEALTH_PATH,
},
})
}
type HealthChecker struct {
ioc.ObjectImpl
ioc_health.HealthCheck
log *zerolog.Logger
Service healthgrpc.HealthServer `ioc:"autowire=true;namespace=controllers"`
}
func (h *HealthChecker) Name() string {
return ioc_health.AppName
}
func (h *HealthChecker) Init() error {
if h.Service == nil {
h.Service = health.NewServer()
}
h.log = log.Sub("health_check")
return nil
}
func (h *HealthChecker) Meta() ioc.ObjectMeta {
meta := ioc.DefaultObjectMeta()
meta.CustomPathPrefix = h.Path
return meta
}
func (h *HealthChecker) Registry(r gin.IRouter) {
r.GET("/", h.HealthHandleFunc)
h.log.Info().Msgf("Get the Health using http://%s%s", http.Get().Addr(), h.Path)
}
func (h *HealthChecker) HealthHandleFunc(c *gin.Context) {
req := ioc_health.NewHealthCheckRequest()
resp, err := h.Service.Check(
c.Request.Context(),
req,
)
if err != nil {
h_response.Failed(c.Writer, err)
return
}
h_response.Success(c.Writer, ioc_health.NewHealth(resp))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-kade/library.git
git@gitee.com:go-kade/library.git
go-kade
library
library
v1.2.4

搜索帮助