generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
health.go
38 lines (27 loc) · 827 Bytes
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
}
}