Skip to content

Commit

Permalink
Customer health and status path and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
rgalanakis committed Feb 10, 2022
1 parent 3834a31 commit 108a0ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
54 changes: 41 additions & 13 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,30 @@ import (
type Config struct {
Logger *logrus.Entry
LoggingMiddlwareConfig LoggingMiddlwareConfig
HealthHandler echo.HandlerFunc
CorsOrigins []string
CorsConfig *middleware.CORSConfig
HealthResponse map[string]interface{}
StatusResponse map[string]interface{}
// Origins for echo's CORS middleware.
// If it and CorsConfig are empty, do not add the middleware.
CorsOrigins []string
// Config for echo's CORS middleware.
// Supercedes CorsOrigins.
// If it and CorsOrigins are empty, do not add the middleware.
CorsConfig *middleware.CORSConfig
// Return this from the health endpoint.
// Defaults to {"o":"k"}.
HealthResponse map[string]interface{}
// Defaults to /healthz.
HealthPath string
// If the health endpoint is not static
// (for example so it can check whether a database is available),
// provide this instead of HealthResponse.
HealthHandler echo.HandlerFunc
// Return this from the status endpoint.
// The default is not very useful so you should provide a value.
StatusResponse map[string]interface{}
// Defaults to /statusz
StatusPath string
// If the status endpoint is not static,
// provide this instead of StatusRespoinse.
StatusHandler echo.HandlerFunc
}

func New(cfg Config) *echo.Echo {
Expand All @@ -46,12 +65,23 @@ func New(cfg Config) *echo.Echo {
return c.JSON(http.StatusOK, cfg.HealthResponse)
}
}
if cfg.StatusResponse == nil {
cfg.StatusResponse = map[string]interface{}{
"version": "not configured",
"message": "you are a lovely and strong person",
if cfg.HealthPath == "" {
cfg.HealthPath = HealthPath
}
if cfg.StatusHandler == nil {
if cfg.StatusResponse == nil {
cfg.StatusResponse = map[string]interface{}{
"version": "not configured",
"message": "you are a lovely and strong person",
}
}
cfg.StatusHandler = func(c echo.Context) error {
return c.JSON(http.StatusOK, cfg.StatusResponse)
}
}
if cfg.StatusPath == "" {
cfg.StatusPath = StatusPath
}
e := echo.New()
e.Logger.SetOutput(os.Stdout)
e.HideBanner = true
Expand All @@ -63,10 +93,8 @@ func New(cfg Config) *echo.Echo {
if cfg.CorsConfig != nil {
e.Use(middleware.CORSWithConfig(*cfg.CorsConfig))
}
e.GET(HealthPath, cfg.HealthHandler)
e.GET(StatusPath, func(c echo.Context) error {
return c.JSON(http.StatusOK, cfg.StatusResponse)
})
e.GET(cfg.HealthPath, cfg.HealthHandler)
e.GET(cfg.StatusPath, cfg.StatusHandler)
return e
}

Expand Down
12 changes: 10 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ var _ = Describe("API", func() {
Expect(rr).To(HaveJsonBody(HaveKeyWithValue("o", "k")))
})

It("can use a custom health handler", func() {
It("can use custom health and status fields", func() {
e = api.New(api.Config{
Logger: logEntry,
HealthHandler: func(c echo.Context) error {
return c.String(200, "yo")
},
HealthPath: "/health",
StatusHandler: func(c echo.Context) error {
return c.String(202, "hai")
},
StatusPath: "/status",
})
rr := Serve(e, GetRequest("/healthz"))
rr := Serve(e, GetRequest("/health"))
Expect(rr).To(HaveResponseCode(200))
Expect(rr.Body.String()).To(Equal("yo"))
rr = Serve(e, GetRequest("/status"))
Expect(rr).To(HaveResponseCode(202))
Expect(rr.Body.String()).To(Equal("hai"))
})

It("has a status endpoint", func() {
Expand Down

0 comments on commit 108a0ed

Please sign in to comment.