Skip to content

Commit

Permalink
Add healthcheck endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwarden committed Sep 7, 2024
1 parent a898c80 commit c06ec1a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"net/http"
"time"
"fmt"

"github.com/pkg/errors"
)

const alertAfterMinutes = 5


func (app app) healthHandler() func(http.ResponseWriter, *http.Request, loginParams) error {
return func(w http.ResponseWriter, r *http.Request, p loginParams) error {

w.Header().Set("Content-Type", "text/plain; charset=utf-8")

lastSampleTime, err := app.ndb.selectLastCrawlTime()
if err != nil {
// Return a 500 error
return errors.Wrap(err, "getting last crawl time")
}

if time.Now().Unix()-int64(lastSampleTime) > alertAfterMinutes*60 {
return fmt.Errorf("last successful crawl of %d is more than %d minutes ago", lastSampleTime, alertAfterMinutes)
}

// write "ok"
_, err = w.Write([]byte("ok"))
if err != nil {
return errors.Wrap(err, "writing response")
}

return nil
}
}

2 changes: 2 additions & 0 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (app app) httpServer(onPanic func(error)) *http.Server {
router.GET("/login", middleware("login", l, onPanic, app.loginHandler()))
router.GET("/logout", middleware("logout", l, onPanic, app.logoutHandler()))

router.GET("/health", middleware("health", l, onPanic, app.healthHandler()))

server.Handler = app.preRouterMiddleware(router, writeTimeout-100*time.Millisecond)

return server
Expand Down

0 comments on commit c06ec1a

Please sign in to comment.