Skip to content

Commit

Permalink
Fix health check
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed May 22, 2021
1 parent d7d7db5 commit 20760e7
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 85 deletions.
26 changes: 26 additions & 0 deletions echo/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
26 changes: 0 additions & 26 deletions echo/health_handler.go

This file was deleted.

26 changes: 26 additions & 0 deletions echo_v3/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
26 changes: 0 additions & 26 deletions echo_v3/health_handler.go

This file was deleted.

26 changes: 26 additions & 0 deletions gin/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
26 changes: 0 additions & 26 deletions gin/health_handler.go

This file was deleted.

2 changes: 1 addition & 1 deletion health/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion health/health_checker.go → health/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
10 changes: 5 additions & 5 deletions health/health_handler.go → health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 20760e7

Please sign in to comment.