Sha256: ac03874646922cb9b7f5c7ea18350503bdc804e91e4e8735a12eec1064175490

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

package betterlog

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/labstack/echo/v4"
)

type Health struct {
	PORT int
}

type Response struct {
	Hostname string `json:"hostname"`
	Healthy  bool   `json:"healthy"`
	Error    string `json:"error,omitempty"`
	Message  string `json:"message,omitempty"`
}

func determineHostname() string {
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "unknown"
	}
	return hostname
}

func (health Health) Check() error {
	return nil
}

func (health Health) ZPage(next echo.HandlerFunc) echo.HandlerFunc {
	hostname := determineHostname()
	return func(c echo.Context) error {
		if c.Path() == "/healthz" {
			if err := health.Check(); err == nil {
				return c.JSON(
					http.StatusOK,
					Response{
						Hostname: hostname,
						Healthy:  true,
					},
				)
			} else {
				log.Printf("error: %v", err)
				return c.JSON(
					http.StatusInternalServerError,
					Response{
						Hostname: hostname,
						Healthy:  false,
						Error:    err.Error(),
						Message:  "problem detected",
					},
				)
			}
		} else {
			return next(c)
		}
	}
}

func StartHealthzEcho(health Health) {
	e := echo.New()

	e.Use(health.ZPage)
	log.Printf("Starting :%d/healthz", health.PORT)
	e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", health.PORT)))
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
betterlog-0.20.3 betterlog/healthz.go
betterlog-0.20.2 betterlog/healthz.go
betterlog-0.20.1 betterlog/healthz.go
betterlog-0.20.0 betterlog/healthz.go
betterlog-0.19.0 betterlog/healthz.go