diff --git a/echo/handler.go b/echo/handler.go new file mode 100644 index 0000000..815bd40 --- /dev/null +++ b/echo/handler.go @@ -0,0 +1,26 @@ +package echo + +import ( + "github.com/core-go/mq/health" + "github.com/labstack/echo/v4" + "net/http" +) + +type Handler struct { + Checkers []health.Checker +} + +func NewHandler(checkers ...health.Checker) *Handler { + return &Handler{checkers} +} + +func (c *Handler) Check() echo.HandlerFunc { + return func(ctx echo.Context) error { + result := health.Check(ctx.Request().Context(), c.Checkers) + if result.Status == health.StatusUp { + return ctx.JSON(http.StatusOK, result) + } else { + return ctx.JSON(http.StatusInternalServerError, result) + } + } +} diff --git a/echo/health_handler.go b/echo/health_handler.go deleted file mode 100644 index d073ab9..0000000 --- a/echo/health_handler.go +++ /dev/null @@ -1,26 +0,0 @@ -package echo - -import ( - "github.com/core-go/health" - "github.com/labstack/echo/v4" - "net/http" -) - -type HealthHandler struct { - HealthCheckers []health.HealthChecker -} - -func NewHealthHandler(checkers ...health.HealthChecker) *HealthHandler { - return &HealthHandler{checkers} -} - -func (c *HealthHandler) Check() echo.HandlerFunc { - return func(ctx echo.Context) error { - result := health.Check(ctx.Request().Context(), c.HealthCheckers) - if result.Status == health.StatusUp { - return ctx.JSON(http.StatusOK, result) - } else { - return ctx.JSON(http.StatusInternalServerError, result) - } - } -} diff --git a/echo_v3/handler.go b/echo_v3/handler.go new file mode 100644 index 0000000..f1af54c --- /dev/null +++ b/echo_v3/handler.go @@ -0,0 +1,26 @@ +package echo + +import ( + "github.com/core-go/mq/health" + "github.com/labstack/echo" + "net/http" +) + +type Handler struct { + Checkers []health.Checker +} + +func NewHandler(checkers ...health.Checker) *Handler { + return &Handler{checkers} +} + +func (c *Handler) Check() echo.HandlerFunc { + return func(ctx echo.Context) error { + result := health.Check(ctx.Request().Context(), c.Checkers) + if result.Status == health.StatusUp { + return ctx.JSON(http.StatusOK, result) + } else { + return ctx.JSON(http.StatusInternalServerError, result) + } + } +} diff --git a/echo_v3/health_handler.go b/echo_v3/health_handler.go deleted file mode 100644 index c93a550..0000000 --- a/echo_v3/health_handler.go +++ /dev/null @@ -1,26 +0,0 @@ -package echo - -import ( - "github.com/core-go/health" - "github.com/labstack/echo" - "net/http" -) - -type HealthHandler struct { - HealthCheckers []health.HealthChecker -} - -func NewHealthHandler(checkers ...health.HealthChecker) *HealthHandler { - return &HealthHandler{checkers} -} - -func (c *HealthHandler) Check() echo.HandlerFunc { - return func(ctx echo.Context) error { - result := health.Check(ctx.Request().Context(), c.HealthCheckers) - if result.Status == health.StatusUp { - return ctx.JSON(http.StatusOK, result) - } else { - return ctx.JSON(http.StatusInternalServerError, result) - } - } -} diff --git a/gin/handler.go b/gin/handler.go new file mode 100644 index 0000000..4e4ea9a --- /dev/null +++ b/gin/handler.go @@ -0,0 +1,26 @@ +package gin + +import ( + "github.com/core-go/mq/health" + "github.com/gin-gonic/gin" + "net/http" +) + +type Handler struct { + Checkers []health.Checker +} + +func NewHandler(checkers ...health.Checker) *Handler { + return &Handler{checkers} +} + +func (c *Handler) Check() gin.HandlerFunc { + return func(ctx *gin.Context) { + result := health.Check(ctx.Request.Context(), c.Checkers) + if result.Status == health.StatusUp { + ctx.JSON(http.StatusOK, result) + } else { + ctx.JSON(http.StatusInternalServerError, result) + } + } +} diff --git a/gin/health_handler.go b/gin/health_handler.go deleted file mode 100644 index 306f4c7..0000000 --- a/gin/health_handler.go +++ /dev/null @@ -1,26 +0,0 @@ -package gin - -import ( - "github.com/core-go/health" - "github.com/gin-gonic/gin" - "net/http" -) - -type HealthHandler struct { - HealthCheckers []health.HealthChecker -} - -func NewHealthHandler(checkers ...health.HealthChecker) *HealthHandler { - return &HealthHandler{checkers} -} - -func (c *HealthHandler) Check() gin.HandlerFunc { - return func(ctx *gin.Context) { - result := health.Check(ctx.Request.Context(), c.HealthCheckers) - if result.Status == health.StatusUp { - ctx.JSON(http.StatusOK, result) - } else { - ctx.JSON(http.StatusInternalServerError, result) - } - } -} diff --git a/health/check.go b/health/check.go index 769634d..64475c4 100644 --- a/health/check.go +++ b/health/check.go @@ -7,7 +7,7 @@ const ( StatusDown = "DOWN" ) -func Check(ctx context.Context, services []HealthChecker) Health { +func Check(ctx context.Context, services []Checker) Health { health := Health{} health.Status = StatusUp healths := make(map[string]Health) diff --git a/health/health_checker.go b/health/checker.go similarity index 86% rename from health/health_checker.go rename to health/checker.go index 7ab43e1..5738781 100644 --- a/health/health_checker.go +++ b/health/checker.go @@ -2,7 +2,7 @@ package health import "context" -type HealthChecker interface { +type Checker interface { Name() string Check(ctx context.Context) (map[string]interface{}, error) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} diff --git a/health/health_handler.go b/health/handler.go similarity index 65% rename from health/health_handler.go rename to health/handler.go index b3ca8d9..7205d18 100644 --- a/health/health_handler.go +++ b/health/handler.go @@ -6,15 +6,15 @@ import ( "net/http" ) -type HealthHandler struct { - Checkers []HealthChecker +type Handler struct { + Checkers []Checker } -func NewHealthHandler(checkers ...HealthChecker) *HealthHandler { - return &HealthHandler{Checkers: checkers} +func NewHandler(checkers ...Checker) *Handler { + return &Handler{Checkers: checkers} } -func (c *HealthHandler) Check(w http.ResponseWriter, r *http.Request) { +func (c *Handler) Check(w http.ResponseWriter, r *http.Request) { ctx := context.Background() h := Check(ctx, c.Checkers) bytes, err := json.Marshal(h)